0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/completion.h>
0010 #include <linux/debugfs.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/mutex.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/slab.h>
0015 #include <linux/workqueue.h>
0016 #include <linux/init.h>
0017 #include <linux/list.h>
0018 #include <linux/regulator/driver.h>
0019 #include <linux/olpc-ec.h>
0020
0021 struct ec_cmd_desc {
0022 u8 cmd;
0023 u8 *inbuf, *outbuf;
0024 size_t inlen, outlen;
0025
0026 int err;
0027 struct completion finished;
0028 struct list_head node;
0029
0030 void *priv;
0031 };
0032
0033 struct olpc_ec_priv {
0034 struct olpc_ec_driver *drv;
0035 u8 version;
0036 struct work_struct worker;
0037 struct mutex cmd_lock;
0038
0039
0040 bool dcon_enabled;
0041
0042
0043 struct list_head cmd_q;
0044 spinlock_t cmd_q_lock;
0045
0046 struct dentry *dbgfs_dir;
0047
0048
0049
0050
0051
0052 u16 ec_wakeup_mask;
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 bool suspended;
0064 };
0065
0066 static struct olpc_ec_driver *ec_driver;
0067 static struct olpc_ec_priv *ec_priv;
0068 static void *ec_cb_arg;
0069
0070 void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
0071 {
0072 ec_driver = drv;
0073 ec_cb_arg = arg;
0074 }
0075 EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
0076
0077 static void olpc_ec_worker(struct work_struct *w)
0078 {
0079 struct olpc_ec_priv *ec = container_of(w, struct olpc_ec_priv, worker);
0080 struct ec_cmd_desc *desc = NULL;
0081 unsigned long flags;
0082
0083
0084 spin_lock_irqsave(&ec->cmd_q_lock, flags);
0085 if (!list_empty(&ec->cmd_q)) {
0086 desc = list_first_entry(&ec->cmd_q, struct ec_cmd_desc, node);
0087 list_del(&desc->node);
0088 }
0089 spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
0090
0091
0092 if (!desc)
0093 return;
0094
0095
0096 mutex_lock(&ec->cmd_lock);
0097 desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
0098 desc->outbuf, desc->outlen, ec_cb_arg);
0099 mutex_unlock(&ec->cmd_lock);
0100
0101
0102 complete(&desc->finished);
0103
0104
0105 schedule_work(&ec->worker);
0106 }
0107
0108
0109
0110
0111
0112 static void queue_ec_descriptor(struct ec_cmd_desc *desc,
0113 struct olpc_ec_priv *ec)
0114 {
0115 unsigned long flags;
0116
0117 INIT_LIST_HEAD(&desc->node);
0118
0119 spin_lock_irqsave(&ec->cmd_q_lock, flags);
0120 list_add_tail(&desc->node, &ec->cmd_q);
0121 spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
0122
0123 schedule_work(&ec->worker);
0124 }
0125
0126 int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
0127 {
0128 struct olpc_ec_priv *ec = ec_priv;
0129 struct ec_cmd_desc desc;
0130
0131
0132 if (!ec_driver)
0133 return -EPROBE_DEFER;
0134
0135 if (WARN_ON(!ec_driver->ec_cmd))
0136 return -ENODEV;
0137
0138 if (!ec)
0139 return -ENOMEM;
0140
0141
0142 if (WARN_ON(ec->suspended))
0143 return -EBUSY;
0144
0145 might_sleep();
0146
0147 desc.cmd = cmd;
0148 desc.inbuf = inbuf;
0149 desc.outbuf = outbuf;
0150 desc.inlen = inlen;
0151 desc.outlen = outlen;
0152 desc.err = 0;
0153 init_completion(&desc.finished);
0154
0155 queue_ec_descriptor(&desc, ec);
0156
0157
0158 wait_for_completion(&desc.finished);
0159
0160
0161 return desc.err;
0162 }
0163 EXPORT_SYMBOL_GPL(olpc_ec_cmd);
0164
0165 void olpc_ec_wakeup_set(u16 value)
0166 {
0167 struct olpc_ec_priv *ec = ec_priv;
0168
0169 if (WARN_ON(!ec))
0170 return;
0171
0172 ec->ec_wakeup_mask |= value;
0173 }
0174 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
0175
0176 void olpc_ec_wakeup_clear(u16 value)
0177 {
0178 struct olpc_ec_priv *ec = ec_priv;
0179
0180 if (WARN_ON(!ec))
0181 return;
0182
0183 ec->ec_wakeup_mask &= ~value;
0184 }
0185 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
0186
0187 int olpc_ec_mask_write(u16 bits)
0188 {
0189 struct olpc_ec_priv *ec = ec_priv;
0190
0191 if (WARN_ON(!ec))
0192 return -ENODEV;
0193
0194
0195 if (ec->version >= 0x5f) {
0196 __be16 ec_word = cpu_to_be16(bits);
0197
0198 return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *)&ec_word, 2, NULL, 0);
0199 } else {
0200 u8 ec_byte = bits & 0xff;
0201
0202 return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
0203 }
0204 }
0205 EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
0206
0207
0208
0209
0210
0211 bool olpc_ec_wakeup_available(void)
0212 {
0213 if (WARN_ON(!ec_driver))
0214 return false;
0215
0216 return ec_driver->wakeup_available;
0217 }
0218 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
0219
0220 int olpc_ec_sci_query(u16 *sci_value)
0221 {
0222 struct olpc_ec_priv *ec = ec_priv;
0223 int ret;
0224
0225 if (WARN_ON(!ec))
0226 return -ENODEV;
0227
0228
0229 if (ec->version >= 0x5f) {
0230 __be16 ec_word;
0231
0232 ret = olpc_ec_cmd(EC_EXT_SCI_QUERY, NULL, 0, (void *)&ec_word, 2);
0233 if (ret == 0)
0234 *sci_value = be16_to_cpu(ec_word);
0235 } else {
0236 u8 ec_byte;
0237
0238 ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
0239 if (ret == 0)
0240 *sci_value = ec_byte;
0241 }
0242
0243 return ret;
0244 }
0245 EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
0246
0247 #ifdef CONFIG_DEBUG_FS
0248
0249
0250
0251
0252
0253
0254 #define EC_MAX_CMD_ARGS (5 + 1)
0255 #define EC_MAX_CMD_REPLY (8)
0256
0257 static DEFINE_MUTEX(ec_dbgfs_lock);
0258 static unsigned char ec_dbgfs_resp[EC_MAX_CMD_REPLY];
0259 static unsigned int ec_dbgfs_resp_bytes;
0260
0261 static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf,
0262 size_t size, loff_t *ppos)
0263 {
0264 int i, m;
0265 unsigned char ec_cmd[EC_MAX_CMD_ARGS];
0266 unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
0267 char cmdbuf[64] = "";
0268 int ec_cmd_bytes;
0269
0270 mutex_lock(&ec_dbgfs_lock);
0271
0272 size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
0273
0274 m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
0275 &ec_dbgfs_resp_bytes, &ec_cmd_int[1], &ec_cmd_int[2],
0276 &ec_cmd_int[3], &ec_cmd_int[4], &ec_cmd_int[5]);
0277 if (m < 2 || ec_dbgfs_resp_bytes > EC_MAX_CMD_REPLY) {
0278
0279 ec_dbgfs_resp_bytes = 0;
0280
0281 pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n");
0282 size = -EINVAL;
0283 goto out;
0284 }
0285
0286
0287 ec_cmd_bytes = m - 2;
0288 for (i = 0; i <= ec_cmd_bytes; i++)
0289 ec_cmd[i] = ec_cmd_int[i];
0290
0291 pr_debug("olpc-ec: debugfs cmd 0x%02x with %d args %5ph, want %d returns\n",
0292 ec_cmd[0], ec_cmd_bytes, ec_cmd + 1,
0293 ec_dbgfs_resp_bytes);
0294
0295 olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
0296 ec_cmd_bytes, ec_dbgfs_resp, ec_dbgfs_resp_bytes);
0297
0298 pr_debug("olpc-ec: response %8ph (%d bytes expected)\n",
0299 ec_dbgfs_resp, ec_dbgfs_resp_bytes);
0300
0301 out:
0302 mutex_unlock(&ec_dbgfs_lock);
0303 return size;
0304 }
0305
0306 static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf,
0307 size_t size, loff_t *ppos)
0308 {
0309 unsigned int i, r;
0310 char *rp;
0311 char respbuf[64];
0312
0313 mutex_lock(&ec_dbgfs_lock);
0314 rp = respbuf;
0315 rp += sprintf(rp, "%02x", ec_dbgfs_resp[0]);
0316 for (i = 1; i < ec_dbgfs_resp_bytes; i++)
0317 rp += sprintf(rp, ", %02x", ec_dbgfs_resp[i]);
0318 mutex_unlock(&ec_dbgfs_lock);
0319 rp += sprintf(rp, "\n");
0320
0321 r = rp - respbuf;
0322 return simple_read_from_buffer(buf, size, ppos, respbuf, r);
0323 }
0324
0325 static const struct file_operations ec_dbgfs_ops = {
0326 .write = ec_dbgfs_cmd_write,
0327 .read = ec_dbgfs_cmd_read,
0328 };
0329
0330 static struct dentry *olpc_ec_setup_debugfs(void)
0331 {
0332 struct dentry *dbgfs_dir;
0333
0334 dbgfs_dir = debugfs_create_dir("olpc-ec", NULL);
0335 if (IS_ERR_OR_NULL(dbgfs_dir))
0336 return NULL;
0337
0338 debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops);
0339
0340 return dbgfs_dir;
0341 }
0342
0343 #else
0344
0345 static struct dentry *olpc_ec_setup_debugfs(void)
0346 {
0347 return NULL;
0348 }
0349
0350 #endif
0351
0352 static int olpc_ec_set_dcon_power(struct olpc_ec_priv *ec, bool state)
0353 {
0354 unsigned char ec_byte = state;
0355 int ret;
0356
0357 if (ec->dcon_enabled == state)
0358 return 0;
0359
0360 ret = olpc_ec_cmd(EC_DCON_POWER_MODE, &ec_byte, 1, NULL, 0);
0361 if (ret)
0362 return ret;
0363
0364 ec->dcon_enabled = state;
0365 return 0;
0366 }
0367
0368 static int dcon_regulator_enable(struct regulator_dev *rdev)
0369 {
0370 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
0371
0372 return olpc_ec_set_dcon_power(ec, true);
0373 }
0374
0375 static int dcon_regulator_disable(struct regulator_dev *rdev)
0376 {
0377 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
0378
0379 return olpc_ec_set_dcon_power(ec, false);
0380 }
0381
0382 static int dcon_regulator_is_enabled(struct regulator_dev *rdev)
0383 {
0384 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
0385
0386 return ec->dcon_enabled ? 1 : 0;
0387 }
0388
0389 static const struct regulator_ops dcon_regulator_ops = {
0390 .enable = dcon_regulator_enable,
0391 .disable = dcon_regulator_disable,
0392 .is_enabled = dcon_regulator_is_enabled,
0393 };
0394
0395 static const struct regulator_desc dcon_desc = {
0396 .name = "dcon",
0397 .id = 0,
0398 .ops = &dcon_regulator_ops,
0399 .type = REGULATOR_VOLTAGE,
0400 .owner = THIS_MODULE,
0401 .enable_time = 25000,
0402 };
0403
0404 static int olpc_ec_probe(struct platform_device *pdev)
0405 {
0406 struct olpc_ec_priv *ec;
0407 struct regulator_config config = { };
0408 struct regulator_dev *regulator;
0409 int err;
0410
0411 if (!ec_driver)
0412 return -ENODEV;
0413
0414 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
0415 if (!ec)
0416 return -ENOMEM;
0417
0418 ec->drv = ec_driver;
0419 INIT_WORK(&ec->worker, olpc_ec_worker);
0420 mutex_init(&ec->cmd_lock);
0421
0422 INIT_LIST_HEAD(&ec->cmd_q);
0423 spin_lock_init(&ec->cmd_q_lock);
0424
0425 ec_priv = ec;
0426 platform_set_drvdata(pdev, ec);
0427
0428
0429 err = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ec->version, 1);
0430 if (err)
0431 goto error;
0432
0433 config.dev = pdev->dev.parent;
0434 config.driver_data = ec;
0435 ec->dcon_enabled = true;
0436 regulator = devm_regulator_register(&pdev->dev, &dcon_desc, &config);
0437 if (IS_ERR(regulator)) {
0438 dev_err(&pdev->dev, "failed to register DCON regulator\n");
0439 err = PTR_ERR(regulator);
0440 goto error;
0441 }
0442
0443 ec->dbgfs_dir = olpc_ec_setup_debugfs();
0444
0445 return 0;
0446
0447 error:
0448 ec_priv = NULL;
0449 kfree(ec);
0450 return err;
0451 }
0452
0453 static int olpc_ec_suspend(struct device *dev)
0454 {
0455 struct platform_device *pdev = to_platform_device(dev);
0456 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
0457 int err = 0;
0458
0459 olpc_ec_mask_write(ec->ec_wakeup_mask);
0460
0461 if (ec_driver->suspend)
0462 err = ec_driver->suspend(pdev);
0463 if (!err)
0464 ec->suspended = true;
0465
0466 return err;
0467 }
0468
0469 static int olpc_ec_resume(struct device *dev)
0470 {
0471 struct platform_device *pdev = to_platform_device(dev);
0472 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
0473
0474 ec->suspended = false;
0475 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
0476 }
0477
0478 static const struct dev_pm_ops olpc_ec_pm_ops = {
0479 .suspend_late = olpc_ec_suspend,
0480 .resume_early = olpc_ec_resume,
0481 };
0482
0483 static struct platform_driver olpc_ec_plat_driver = {
0484 .probe = olpc_ec_probe,
0485 .driver = {
0486 .name = "olpc-ec",
0487 .pm = &olpc_ec_pm_ops,
0488 },
0489 };
0490
0491 static int __init olpc_ec_init_module(void)
0492 {
0493 return platform_driver_register(&olpc_ec_plat_driver);
0494 }
0495 arch_initcall(olpc_ec_init_module);