aat2870-core.c 11 KB

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