aat2870-core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * linux/drivers/mfd/aat2870-core.c
  3. *
  4. * Copyright (c) 2011, NVIDIA Corporation.
  5. * Author: Jin Park <jinyoungp@nvidia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/slab.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/i2c.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/mfd/core.h>
  31. #include <linux/mfd/aat2870.h>
  32. #include <linux/regulator/machine.h>
  33. static struct aat2870_register aat2870_regs[AAT2870_REG_NUM] = {
  34. /* readable, writeable, value */
  35. { 0, 1, 0x00 }, /* 0x00 AAT2870_BL_CH_EN */
  36. { 0, 1, 0x16 }, /* 0x01 AAT2870_BLM */
  37. { 0, 1, 0x16 }, /* 0x02 AAT2870_BLS */
  38. { 0, 1, 0x56 }, /* 0x03 AAT2870_BL1 */
  39. { 0, 1, 0x56 }, /* 0x04 AAT2870_BL2 */
  40. { 0, 1, 0x56 }, /* 0x05 AAT2870_BL3 */
  41. { 0, 1, 0x56 }, /* 0x06 AAT2870_BL4 */
  42. { 0, 1, 0x56 }, /* 0x07 AAT2870_BL5 */
  43. { 0, 1, 0x56 }, /* 0x08 AAT2870_BL6 */
  44. { 0, 1, 0x56 }, /* 0x09 AAT2870_BL7 */
  45. { 0, 1, 0x56 }, /* 0x0A AAT2870_BL8 */
  46. { 0, 1, 0x00 }, /* 0x0B AAT2870_FLR */
  47. { 0, 1, 0x03 }, /* 0x0C AAT2870_FM */
  48. { 0, 1, 0x03 }, /* 0x0D AAT2870_FS */
  49. { 0, 1, 0x10 }, /* 0x0E AAT2870_ALS_CFG0 */
  50. { 0, 1, 0x06 }, /* 0x0F AAT2870_ALS_CFG1 */
  51. { 0, 1, 0x00 }, /* 0x10 AAT2870_ALS_CFG2 */
  52. { 1, 0, 0x00 }, /* 0x11 AAT2870_AMB */
  53. { 0, 1, 0x00 }, /* 0x12 AAT2870_ALS0 */
  54. { 0, 1, 0x00 }, /* 0x13 AAT2870_ALS1 */
  55. { 0, 1, 0x00 }, /* 0x14 AAT2870_ALS2 */
  56. { 0, 1, 0x00 }, /* 0x15 AAT2870_ALS3 */
  57. { 0, 1, 0x00 }, /* 0x16 AAT2870_ALS4 */
  58. { 0, 1, 0x00 }, /* 0x17 AAT2870_ALS5 */
  59. { 0, 1, 0x00 }, /* 0x18 AAT2870_ALS6 */
  60. { 0, 1, 0x00 }, /* 0x19 AAT2870_ALS7 */
  61. { 0, 1, 0x00 }, /* 0x1A AAT2870_ALS8 */
  62. { 0, 1, 0x00 }, /* 0x1B AAT2870_ALS9 */
  63. { 0, 1, 0x00 }, /* 0x1C AAT2870_ALSA */
  64. { 0, 1, 0x00 }, /* 0x1D AAT2870_ALSB */
  65. { 0, 1, 0x00 }, /* 0x1E AAT2870_ALSC */
  66. { 0, 1, 0x00 }, /* 0x1F AAT2870_ALSD */
  67. { 0, 1, 0x00 }, /* 0x20 AAT2870_ALSE */
  68. { 0, 1, 0x00 }, /* 0x21 AAT2870_ALSF */
  69. { 0, 1, 0x00 }, /* 0x22 AAT2870_SUB_SET */
  70. { 0, 1, 0x00 }, /* 0x23 AAT2870_SUB_CTRL */
  71. { 0, 1, 0x00 }, /* 0x24 AAT2870_LDO_AB */
  72. { 0, 1, 0x00 }, /* 0x25 AAT2870_LDO_CD */
  73. { 0, 1, 0x00 }, /* 0x26 AAT2870_LDO_EN */
  74. };
  75. static struct mfd_cell aat2870_devs[] = {
  76. {
  77. .name = "aat2870-backlight",
  78. .id = AAT2870_ID_BL,
  79. .pdata_size = sizeof(struct aat2870_bl_platform_data),
  80. },
  81. {
  82. .name = "aat2870-regulator",
  83. .id = AAT2870_ID_LDOA,
  84. .pdata_size = sizeof(struct regulator_init_data),
  85. },
  86. {
  87. .name = "aat2870-regulator",
  88. .id = AAT2870_ID_LDOB,
  89. .pdata_size = sizeof(struct regulator_init_data),
  90. },
  91. {
  92. .name = "aat2870-regulator",
  93. .id = AAT2870_ID_LDOC,
  94. .pdata_size = sizeof(struct regulator_init_data),
  95. },
  96. {
  97. .name = "aat2870-regulator",
  98. .id = AAT2870_ID_LDOD,
  99. .pdata_size = sizeof(struct regulator_init_data),
  100. },
  101. };
  102. static int __aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
  103. {
  104. int ret;
  105. if (addr >= AAT2870_REG_NUM) {
  106. dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
  107. return -EINVAL;
  108. }
  109. if (!aat2870->reg_cache[addr].readable) {
  110. *val = aat2870->reg_cache[addr].value;
  111. goto out;
  112. }
  113. ret = i2c_master_send(aat2870->client, &addr, 1);
  114. if (ret < 0)
  115. return ret;
  116. if (ret != 1)
  117. return -EIO;
  118. ret = i2c_master_recv(aat2870->client, val, 1);
  119. if (ret < 0)
  120. return ret;
  121. if (ret != 1)
  122. return -EIO;
  123. out:
  124. dev_dbg(aat2870->dev, "read: addr=0x%02x, val=0x%02x\n", addr, *val);
  125. return 0;
  126. }
  127. static int __aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
  128. {
  129. u8 msg[2];
  130. int ret;
  131. if (addr >= AAT2870_REG_NUM) {
  132. dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
  133. return -EINVAL;
  134. }
  135. if (!aat2870->reg_cache[addr].writeable) {
  136. dev_err(aat2870->dev, "Address 0x%02x is not writeable\n",
  137. addr);
  138. return -EINVAL;
  139. }
  140. msg[0] = addr;
  141. msg[1] = val;
  142. ret = i2c_master_send(aat2870->client, msg, 2);
  143. if (ret < 0)
  144. return ret;
  145. if (ret != 2)
  146. return -EIO;
  147. aat2870->reg_cache[addr].value = val;
  148. dev_dbg(aat2870->dev, "write: addr=0x%02x, val=0x%02x\n", addr, val);
  149. return 0;
  150. }
  151. static int aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
  152. {
  153. int ret;
  154. mutex_lock(&aat2870->io_lock);
  155. ret = __aat2870_read(aat2870, addr, val);
  156. mutex_unlock(&aat2870->io_lock);
  157. return ret;
  158. }
  159. static int aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
  160. {
  161. int ret;
  162. mutex_lock(&aat2870->io_lock);
  163. ret = __aat2870_write(aat2870, addr, val);
  164. mutex_unlock(&aat2870->io_lock);
  165. return ret;
  166. }
  167. static int aat2870_update(struct aat2870_data *aat2870, u8 addr, u8 mask,
  168. u8 val)
  169. {
  170. int change;
  171. u8 old_val, new_val;
  172. int ret;
  173. mutex_lock(&aat2870->io_lock);
  174. ret = __aat2870_read(aat2870, addr, &old_val);
  175. if (ret)
  176. goto out_unlock;
  177. new_val = (old_val & ~mask) | (val & mask);
  178. change = old_val != new_val;
  179. if (change)
  180. ret = __aat2870_write(aat2870, addr, new_val);
  181. out_unlock:
  182. mutex_unlock(&aat2870->io_lock);
  183. return ret;
  184. }
  185. static inline void aat2870_enable(struct aat2870_data *aat2870)
  186. {
  187. if (aat2870->en_pin >= 0)
  188. gpio_set_value(aat2870->en_pin, 1);
  189. aat2870->is_enable = 1;
  190. }
  191. static inline void aat2870_disable(struct aat2870_data *aat2870)
  192. {
  193. if (aat2870->en_pin >= 0)
  194. gpio_set_value(aat2870->en_pin, 0);
  195. aat2870->is_enable = 0;
  196. }
  197. #ifdef CONFIG_DEBUG_FS
  198. static ssize_t aat2870_dump_reg(struct aat2870_data *aat2870, char *buf)
  199. {
  200. u8 addr, val;
  201. ssize_t count = 0;
  202. int ret;
  203. count += sprintf(buf, "aat2870 registers\n");
  204. for (addr = 0; addr < AAT2870_REG_NUM; addr++) {
  205. count += sprintf(buf + count, "0x%02x: ", addr);
  206. if (count >= PAGE_SIZE - 1)
  207. break;
  208. ret = aat2870->read(aat2870, addr, &val);
  209. if (ret == 0)
  210. count += snprintf(buf + count, PAGE_SIZE - count,
  211. "0x%02x", val);
  212. else
  213. count += snprintf(buf + count, PAGE_SIZE - count,
  214. "<read fail: %d>", ret);
  215. if (count >= PAGE_SIZE - 1)
  216. break;
  217. count += snprintf(buf + count, PAGE_SIZE - count, "\n");
  218. if (count >= PAGE_SIZE - 1)
  219. break;
  220. }
  221. /* Truncate count; min() would cause a warning */
  222. if (count >= PAGE_SIZE)
  223. count = PAGE_SIZE - 1;
  224. return count;
  225. }
  226. static ssize_t aat2870_reg_read_file(struct file *file, char __user *user_buf,
  227. size_t count, loff_t *ppos)
  228. {
  229. struct aat2870_data *aat2870 = file->private_data;
  230. char *buf;
  231. ssize_t ret;
  232. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  233. if (!buf)
  234. return -ENOMEM;
  235. ret = aat2870_dump_reg(aat2870, buf);
  236. if (ret >= 0)
  237. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  238. kfree(buf);
  239. return ret;
  240. }
  241. static ssize_t aat2870_reg_write_file(struct file *file,
  242. const char __user *user_buf, size_t count,
  243. loff_t *ppos)
  244. {
  245. struct aat2870_data *aat2870 = file->private_data;
  246. char buf[32];
  247. ssize_t buf_size;
  248. char *start = buf;
  249. unsigned long addr, val;
  250. int ret;
  251. buf_size = min(count, (size_t)(sizeof(buf)-1));
  252. if (copy_from_user(buf, user_buf, buf_size)) {
  253. dev_err(aat2870->dev, "Failed to copy from user\n");
  254. return -EFAULT;
  255. }
  256. buf[buf_size] = 0;
  257. while (*start == ' ')
  258. start++;
  259. ret = kstrtoul(start, 16, &addr);
  260. if (ret)
  261. return ret;
  262. if (addr >= AAT2870_REG_NUM) {
  263. dev_err(aat2870->dev, "Invalid address, 0x%lx\n", addr);
  264. return -EINVAL;
  265. }
  266. while (*start == ' ')
  267. start++;
  268. ret = kstrtoul(start, 16, &val);
  269. if (ret)
  270. return ret;
  271. ret = aat2870->write(aat2870, (u8)addr, (u8)val);
  272. if (ret)
  273. return ret;
  274. return buf_size;
  275. }
  276. static const struct file_operations aat2870_reg_fops = {
  277. .open = simple_open,
  278. .read = aat2870_reg_read_file,
  279. .write = aat2870_reg_write_file,
  280. };
  281. static void aat2870_init_debugfs(struct aat2870_data *aat2870)
  282. {
  283. aat2870->dentry_root = debugfs_create_dir("aat2870", NULL);
  284. if (!aat2870->dentry_root) {
  285. dev_warn(aat2870->dev,
  286. "Failed to create debugfs root directory\n");
  287. return;
  288. }
  289. aat2870->dentry_reg = debugfs_create_file("regs", 0644,
  290. aat2870->dentry_root,
  291. aat2870, &aat2870_reg_fops);
  292. if (!aat2870->dentry_reg)
  293. dev_warn(aat2870->dev,
  294. "Failed to create debugfs register file\n");
  295. }
  296. static void aat2870_uninit_debugfs(struct aat2870_data *aat2870)
  297. {
  298. debugfs_remove_recursive(aat2870->dentry_root);
  299. }
  300. #else
  301. static inline void aat2870_init_debugfs(struct aat2870_data *aat2870)
  302. {
  303. }
  304. static inline void aat2870_uninit_debugfs(struct aat2870_data *aat2870)
  305. {
  306. }
  307. #endif /* CONFIG_DEBUG_FS */
  308. static int aat2870_i2c_probe(struct i2c_client *client,
  309. const struct i2c_device_id *id)
  310. {
  311. struct aat2870_platform_data *pdata = dev_get_platdata(&client->dev);
  312. struct aat2870_data *aat2870;
  313. int i, j;
  314. int ret = 0;
  315. aat2870 = devm_kzalloc(&client->dev, sizeof(struct aat2870_data),
  316. GFP_KERNEL);
  317. if (!aat2870) {
  318. dev_err(&client->dev,
  319. "Failed to allocate memory for aat2870\n");
  320. return -ENOMEM;
  321. }
  322. aat2870->dev = &client->dev;
  323. dev_set_drvdata(aat2870->dev, aat2870);
  324. aat2870->client = client;
  325. i2c_set_clientdata(client, aat2870);
  326. aat2870->reg_cache = aat2870_regs;
  327. if (pdata->en_pin < 0)
  328. aat2870->en_pin = -1;
  329. else
  330. aat2870->en_pin = pdata->en_pin;
  331. aat2870->init = pdata->init;
  332. aat2870->uninit = pdata->uninit;
  333. aat2870->read = aat2870_read;
  334. aat2870->write = aat2870_write;
  335. aat2870->update = aat2870_update;
  336. mutex_init(&aat2870->io_lock);
  337. if (aat2870->init)
  338. aat2870->init(aat2870);
  339. if (aat2870->en_pin >= 0) {
  340. ret = devm_gpio_request_one(&client->dev, aat2870->en_pin,
  341. GPIOF_OUT_INIT_HIGH, "aat2870-en");
  342. if (ret < 0) {
  343. dev_err(&client->dev,
  344. "Failed to request GPIO %d\n", aat2870->en_pin);
  345. return ret;
  346. }
  347. }
  348. aat2870_enable(aat2870);
  349. for (i = 0; i < pdata->num_subdevs; i++) {
  350. for (j = 0; j < ARRAY_SIZE(aat2870_devs); j++) {
  351. if ((pdata->subdevs[i].id == aat2870_devs[j].id) &&
  352. !strcmp(pdata->subdevs[i].name,
  353. aat2870_devs[j].name)) {
  354. aat2870_devs[j].platform_data =
  355. pdata->subdevs[i].platform_data;
  356. break;
  357. }
  358. }
  359. }
  360. ret = mfd_add_devices(aat2870->dev, 0, aat2870_devs,
  361. ARRAY_SIZE(aat2870_devs), NULL, 0, NULL);
  362. if (ret != 0) {
  363. dev_err(aat2870->dev, "Failed to add subdev: %d\n", ret);
  364. goto out_disable;
  365. }
  366. aat2870_init_debugfs(aat2870);
  367. return 0;
  368. out_disable:
  369. aat2870_disable(aat2870);
  370. return ret;
  371. }
  372. static int aat2870_i2c_remove(struct i2c_client *client)
  373. {
  374. struct aat2870_data *aat2870 = i2c_get_clientdata(client);
  375. aat2870_uninit_debugfs(aat2870);
  376. mfd_remove_devices(aat2870->dev);
  377. aat2870_disable(aat2870);
  378. if (aat2870->uninit)
  379. aat2870->uninit(aat2870);
  380. return 0;
  381. }
  382. #ifdef CONFIG_PM_SLEEP
  383. static int aat2870_i2c_suspend(struct device *dev)
  384. {
  385. struct i2c_client *client = to_i2c_client(dev);
  386. struct aat2870_data *aat2870 = i2c_get_clientdata(client);
  387. aat2870_disable(aat2870);
  388. return 0;
  389. }
  390. static int aat2870_i2c_resume(struct device *dev)
  391. {
  392. struct i2c_client *client = to_i2c_client(dev);
  393. struct aat2870_data *aat2870 = i2c_get_clientdata(client);
  394. struct aat2870_register *reg = NULL;
  395. int i;
  396. aat2870_enable(aat2870);
  397. /* restore registers */
  398. for (i = 0; i < AAT2870_REG_NUM; i++) {
  399. reg = &aat2870->reg_cache[i];
  400. if (reg->writeable)
  401. aat2870->write(aat2870, i, reg->value);
  402. }
  403. return 0;
  404. }
  405. #endif /* CONFIG_PM_SLEEP */
  406. static SIMPLE_DEV_PM_OPS(aat2870_pm_ops, aat2870_i2c_suspend,
  407. aat2870_i2c_resume);
  408. static const struct i2c_device_id aat2870_i2c_id_table[] = {
  409. { "aat2870", 0 },
  410. { }
  411. };
  412. MODULE_DEVICE_TABLE(i2c, aat2870_i2c_id_table);
  413. static struct i2c_driver aat2870_i2c_driver = {
  414. .driver = {
  415. .name = "aat2870",
  416. .pm = &aat2870_pm_ops,
  417. },
  418. .probe = aat2870_i2c_probe,
  419. .remove = aat2870_i2c_remove,
  420. .id_table = aat2870_i2c_id_table,
  421. };
  422. static int __init aat2870_init(void)
  423. {
  424. return i2c_add_driver(&aat2870_i2c_driver);
  425. }
  426. subsys_initcall(aat2870_init);
  427. static void __exit aat2870_exit(void)
  428. {
  429. i2c_del_driver(&aat2870_i2c_driver);
  430. }
  431. module_exit(aat2870_exit);
  432. MODULE_DESCRIPTION("Core support for the AnalogicTech AAT2870");
  433. MODULE_LICENSE("GPL");
  434. MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");