| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Generic driver for the OLPC Embedded Controller. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Author: Andres Salomon <dilinger@queued.net> |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Copyright (C) 2011-2012 One Laptop per Child Foundation. |
|---|
| 7 | | - * |
|---|
| 8 | | - * Licensed under the GPL v2 or later. |
|---|
| 9 | 8 | */ |
|---|
| 10 | 9 | #include <linux/completion.h> |
|---|
| 11 | 10 | #include <linux/debugfs.h> |
|---|
| .. | .. |
|---|
| 16 | 15 | #include <linux/workqueue.h> |
|---|
| 17 | 16 | #include <linux/init.h> |
|---|
| 18 | 17 | #include <linux/list.h> |
|---|
| 18 | +#include <linux/regulator/driver.h> |
|---|
| 19 | 19 | #include <linux/olpc-ec.h> |
|---|
| 20 | | -#include <asm/olpc.h> |
|---|
| 21 | 20 | |
|---|
| 22 | 21 | struct ec_cmd_desc { |
|---|
| 23 | 22 | u8 cmd; |
|---|
| .. | .. |
|---|
| 33 | 32 | |
|---|
| 34 | 33 | struct olpc_ec_priv { |
|---|
| 35 | 34 | struct olpc_ec_driver *drv; |
|---|
| 35 | + u8 version; |
|---|
| 36 | 36 | struct work_struct worker; |
|---|
| 37 | 37 | struct mutex cmd_lock; |
|---|
| 38 | + |
|---|
| 39 | + /* DCON regulator */ |
|---|
| 40 | + struct regulator_dev *dcon_rdev; |
|---|
| 41 | + bool dcon_enabled; |
|---|
| 38 | 42 | |
|---|
| 39 | 43 | /* Pending EC commands */ |
|---|
| 40 | 44 | struct list_head cmd_q; |
|---|
| 41 | 45 | spinlock_t cmd_q_lock; |
|---|
| 42 | 46 | |
|---|
| 43 | 47 | struct dentry *dbgfs_dir; |
|---|
| 48 | + |
|---|
| 49 | + /* |
|---|
| 50 | + * EC event mask to be applied during suspend (defining wakeup |
|---|
| 51 | + * sources). |
|---|
| 52 | + */ |
|---|
| 53 | + u16 ec_wakeup_mask; |
|---|
| 44 | 54 | |
|---|
| 45 | 55 | /* |
|---|
| 46 | 56 | * Running an EC command while suspending means we don't always finish |
|---|
| .. | .. |
|---|
| 119 | 129 | struct olpc_ec_priv *ec = ec_priv; |
|---|
| 120 | 130 | struct ec_cmd_desc desc; |
|---|
| 121 | 131 | |
|---|
| 122 | | - /* Ensure a driver and ec hook have been registered */ |
|---|
| 123 | | - if (WARN_ON(!ec_driver || !ec_driver->ec_cmd)) |
|---|
| 132 | + /* Driver not yet registered. */ |
|---|
| 133 | + if (!ec_driver) |
|---|
| 134 | + return -EPROBE_DEFER; |
|---|
| 135 | + |
|---|
| 136 | + if (WARN_ON(!ec_driver->ec_cmd)) |
|---|
| 124 | 137 | return -ENODEV; |
|---|
| 125 | 138 | |
|---|
| 126 | 139 | if (!ec) |
|---|
| .. | .. |
|---|
| 150 | 163 | } |
|---|
| 151 | 164 | EXPORT_SYMBOL_GPL(olpc_ec_cmd); |
|---|
| 152 | 165 | |
|---|
| 166 | +void olpc_ec_wakeup_set(u16 value) |
|---|
| 167 | +{ |
|---|
| 168 | + struct olpc_ec_priv *ec = ec_priv; |
|---|
| 169 | + |
|---|
| 170 | + if (WARN_ON(!ec)) |
|---|
| 171 | + return; |
|---|
| 172 | + |
|---|
| 173 | + ec->ec_wakeup_mask |= value; |
|---|
| 174 | +} |
|---|
| 175 | +EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set); |
|---|
| 176 | + |
|---|
| 177 | +void olpc_ec_wakeup_clear(u16 value) |
|---|
| 178 | +{ |
|---|
| 179 | + struct olpc_ec_priv *ec = ec_priv; |
|---|
| 180 | + |
|---|
| 181 | + if (WARN_ON(!ec)) |
|---|
| 182 | + return; |
|---|
| 183 | + |
|---|
| 184 | + ec->ec_wakeup_mask &= ~value; |
|---|
| 185 | +} |
|---|
| 186 | +EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear); |
|---|
| 187 | + |
|---|
| 188 | +int olpc_ec_mask_write(u16 bits) |
|---|
| 189 | +{ |
|---|
| 190 | + struct olpc_ec_priv *ec = ec_priv; |
|---|
| 191 | + |
|---|
| 192 | + if (WARN_ON(!ec)) |
|---|
| 193 | + return -ENODEV; |
|---|
| 194 | + |
|---|
| 195 | + /* EC version 0x5f adds support for wide SCI mask */ |
|---|
| 196 | + if (ec->version >= 0x5f) { |
|---|
| 197 | + __be16 ec_word = cpu_to_be16(bits); |
|---|
| 198 | + |
|---|
| 199 | + return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *)&ec_word, 2, NULL, 0); |
|---|
| 200 | + } else { |
|---|
| 201 | + u8 ec_byte = bits & 0xff; |
|---|
| 202 | + |
|---|
| 203 | + return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0); |
|---|
| 204 | + } |
|---|
| 205 | +} |
|---|
| 206 | +EXPORT_SYMBOL_GPL(olpc_ec_mask_write); |
|---|
| 207 | + |
|---|
| 208 | +/* |
|---|
| 209 | + * Returns true if the compile and runtime configurations allow for EC events |
|---|
| 210 | + * to wake the system. |
|---|
| 211 | + */ |
|---|
| 212 | +bool olpc_ec_wakeup_available(void) |
|---|
| 213 | +{ |
|---|
| 214 | + if (WARN_ON(!ec_driver)) |
|---|
| 215 | + return false; |
|---|
| 216 | + |
|---|
| 217 | + return ec_driver->wakeup_available; |
|---|
| 218 | +} |
|---|
| 219 | +EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available); |
|---|
| 220 | + |
|---|
| 221 | +int olpc_ec_sci_query(u16 *sci_value) |
|---|
| 222 | +{ |
|---|
| 223 | + struct olpc_ec_priv *ec = ec_priv; |
|---|
| 224 | + int ret; |
|---|
| 225 | + |
|---|
| 226 | + if (WARN_ON(!ec)) |
|---|
| 227 | + return -ENODEV; |
|---|
| 228 | + |
|---|
| 229 | + /* EC version 0x5f adds support for wide SCI mask */ |
|---|
| 230 | + if (ec->version >= 0x5f) { |
|---|
| 231 | + __be16 ec_word; |
|---|
| 232 | + |
|---|
| 233 | + ret = olpc_ec_cmd(EC_EXT_SCI_QUERY, NULL, 0, (void *)&ec_word, 2); |
|---|
| 234 | + if (ret == 0) |
|---|
| 235 | + *sci_value = be16_to_cpu(ec_word); |
|---|
| 236 | + } else { |
|---|
| 237 | + u8 ec_byte; |
|---|
| 238 | + |
|---|
| 239 | + ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1); |
|---|
| 240 | + if (ret == 0) |
|---|
| 241 | + *sci_value = ec_byte; |
|---|
| 242 | + } |
|---|
| 243 | + |
|---|
| 244 | + return ret; |
|---|
| 245 | +} |
|---|
| 246 | +EXPORT_SYMBOL_GPL(olpc_ec_sci_query); |
|---|
| 247 | + |
|---|
| 153 | 248 | #ifdef CONFIG_DEBUG_FS |
|---|
| 154 | 249 | |
|---|
| 155 | 250 | /* |
|---|
| .. | .. |
|---|
| 170 | 265 | int i, m; |
|---|
| 171 | 266 | unsigned char ec_cmd[EC_MAX_CMD_ARGS]; |
|---|
| 172 | 267 | unsigned int ec_cmd_int[EC_MAX_CMD_ARGS]; |
|---|
| 173 | | - char cmdbuf[64]; |
|---|
| 268 | + char cmdbuf[64] = ""; |
|---|
| 174 | 269 | int ec_cmd_bytes; |
|---|
| 175 | 270 | |
|---|
| 176 | 271 | mutex_lock(&ec_dbgfs_lock); |
|---|
| .. | .. |
|---|
| 255 | 350 | |
|---|
| 256 | 351 | #endif /* CONFIG_DEBUG_FS */ |
|---|
| 257 | 352 | |
|---|
| 353 | +static int olpc_ec_set_dcon_power(struct olpc_ec_priv *ec, bool state) |
|---|
| 354 | +{ |
|---|
| 355 | + unsigned char ec_byte = state; |
|---|
| 356 | + int ret; |
|---|
| 357 | + |
|---|
| 358 | + if (ec->dcon_enabled == state) |
|---|
| 359 | + return 0; |
|---|
| 360 | + |
|---|
| 361 | + ret = olpc_ec_cmd(EC_DCON_POWER_MODE, &ec_byte, 1, NULL, 0); |
|---|
| 362 | + if (ret) |
|---|
| 363 | + return ret; |
|---|
| 364 | + |
|---|
| 365 | + ec->dcon_enabled = state; |
|---|
| 366 | + return 0; |
|---|
| 367 | +} |
|---|
| 368 | + |
|---|
| 369 | +static int dcon_regulator_enable(struct regulator_dev *rdev) |
|---|
| 370 | +{ |
|---|
| 371 | + struct olpc_ec_priv *ec = rdev_get_drvdata(rdev); |
|---|
| 372 | + |
|---|
| 373 | + return olpc_ec_set_dcon_power(ec, true); |
|---|
| 374 | +} |
|---|
| 375 | + |
|---|
| 376 | +static int dcon_regulator_disable(struct regulator_dev *rdev) |
|---|
| 377 | +{ |
|---|
| 378 | + struct olpc_ec_priv *ec = rdev_get_drvdata(rdev); |
|---|
| 379 | + |
|---|
| 380 | + return olpc_ec_set_dcon_power(ec, false); |
|---|
| 381 | +} |
|---|
| 382 | + |
|---|
| 383 | +static int dcon_regulator_is_enabled(struct regulator_dev *rdev) |
|---|
| 384 | +{ |
|---|
| 385 | + struct olpc_ec_priv *ec = rdev_get_drvdata(rdev); |
|---|
| 386 | + |
|---|
| 387 | + return ec->dcon_enabled ? 1 : 0; |
|---|
| 388 | +} |
|---|
| 389 | + |
|---|
| 390 | +static struct regulator_ops dcon_regulator_ops = { |
|---|
| 391 | + .enable = dcon_regulator_enable, |
|---|
| 392 | + .disable = dcon_regulator_disable, |
|---|
| 393 | + .is_enabled = dcon_regulator_is_enabled, |
|---|
| 394 | +}; |
|---|
| 395 | + |
|---|
| 396 | +static const struct regulator_desc dcon_desc = { |
|---|
| 397 | + .name = "dcon", |
|---|
| 398 | + .id = 0, |
|---|
| 399 | + .ops = &dcon_regulator_ops, |
|---|
| 400 | + .type = REGULATOR_VOLTAGE, |
|---|
| 401 | + .owner = THIS_MODULE, |
|---|
| 402 | +}; |
|---|
| 403 | + |
|---|
| 258 | 404 | static int olpc_ec_probe(struct platform_device *pdev) |
|---|
| 259 | 405 | { |
|---|
| 260 | 406 | struct olpc_ec_priv *ec; |
|---|
| 407 | + struct regulator_config config = { }; |
|---|
| 261 | 408 | int err; |
|---|
| 262 | 409 | |
|---|
| 263 | 410 | if (!ec_driver) |
|---|
| .. | .. |
|---|
| 277 | 424 | ec_priv = ec; |
|---|
| 278 | 425 | platform_set_drvdata(pdev, ec); |
|---|
| 279 | 426 | |
|---|
| 280 | | - err = ec_driver->probe ? ec_driver->probe(pdev) : 0; |
|---|
| 281 | | - if (err) { |
|---|
| 282 | | - ec_priv = NULL; |
|---|
| 283 | | - kfree(ec); |
|---|
| 284 | | - } else { |
|---|
| 285 | | - ec->dbgfs_dir = olpc_ec_setup_debugfs(); |
|---|
| 427 | + /* get the EC revision */ |
|---|
| 428 | + err = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ec->version, 1); |
|---|
| 429 | + if (err) |
|---|
| 430 | + goto error; |
|---|
| 431 | + |
|---|
| 432 | + config.dev = pdev->dev.parent; |
|---|
| 433 | + config.driver_data = ec; |
|---|
| 434 | + ec->dcon_enabled = true; |
|---|
| 435 | + ec->dcon_rdev = devm_regulator_register(&pdev->dev, &dcon_desc, |
|---|
| 436 | + &config); |
|---|
| 437 | + if (IS_ERR(ec->dcon_rdev)) { |
|---|
| 438 | + dev_err(&pdev->dev, "failed to register DCON regulator\n"); |
|---|
| 439 | + err = PTR_ERR(ec->dcon_rdev); |
|---|
| 440 | + goto error; |
|---|
| 286 | 441 | } |
|---|
| 287 | 442 | |
|---|
| 443 | + ec->dbgfs_dir = olpc_ec_setup_debugfs(); |
|---|
| 444 | + |
|---|
| 445 | + return 0; |
|---|
| 446 | + |
|---|
| 447 | +error: |
|---|
| 448 | + ec_priv = NULL; |
|---|
| 449 | + kfree(ec); |
|---|
| 288 | 450 | return err; |
|---|
| 289 | 451 | } |
|---|
| 290 | 452 | |
|---|
| .. | .. |
|---|
| 294 | 456 | struct olpc_ec_priv *ec = platform_get_drvdata(pdev); |
|---|
| 295 | 457 | int err = 0; |
|---|
| 296 | 458 | |
|---|
| 459 | + olpc_ec_mask_write(ec->ec_wakeup_mask); |
|---|
| 460 | + |
|---|
| 297 | 461 | if (ec_driver->suspend) |
|---|
| 298 | 462 | err = ec_driver->suspend(pdev); |
|---|
| 299 | 463 | if (!err) |
|---|