tc3589x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License, version 2
  5. * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
  6. * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  7. */
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/mfd/core.h>
  17. #include <linux/mfd/tc3589x.h>
  18. #include <linux/err.h>
  19. /**
  20. * enum tc3589x_version - indicates the TC3589x version
  21. */
  22. enum tc3589x_version {
  23. TC3589X_TC35890,
  24. TC3589X_TC35892,
  25. TC3589X_TC35893,
  26. TC3589X_TC35894,
  27. TC3589X_TC35895,
  28. TC3589X_TC35896,
  29. TC3589X_UNKNOWN,
  30. };
  31. #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
  32. #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
  33. /**
  34. * tc3589x_reg_read() - read a single TC3589x register
  35. * @tc3589x: Device to read from
  36. * @reg: Register to read
  37. */
  38. int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
  39. {
  40. int ret;
  41. ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
  42. if (ret < 0)
  43. dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
  44. reg, ret);
  45. return ret;
  46. }
  47. EXPORT_SYMBOL_GPL(tc3589x_reg_read);
  48. /**
  49. * tc3589x_reg_read() - write a single TC3589x register
  50. * @tc3589x: Device to write to
  51. * @reg: Register to read
  52. * @data: Value to write
  53. */
  54. int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
  55. {
  56. int ret;
  57. ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
  58. if (ret < 0)
  59. dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
  60. reg, ret);
  61. return ret;
  62. }
  63. EXPORT_SYMBOL_GPL(tc3589x_reg_write);
  64. /**
  65. * tc3589x_block_read() - read multiple TC3589x registers
  66. * @tc3589x: Device to read from
  67. * @reg: First register
  68. * @length: Number of registers
  69. * @values: Buffer to write to
  70. */
  71. int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
  72. {
  73. int ret;
  74. ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
  75. if (ret < 0)
  76. dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
  77. reg, ret);
  78. return ret;
  79. }
  80. EXPORT_SYMBOL_GPL(tc3589x_block_read);
  81. /**
  82. * tc3589x_block_write() - write multiple TC3589x registers
  83. * @tc3589x: Device to write to
  84. * @reg: First register
  85. * @length: Number of registers
  86. * @values: Values to write
  87. */
  88. int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
  89. const u8 *values)
  90. {
  91. int ret;
  92. ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
  93. values);
  94. if (ret < 0)
  95. dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
  96. reg, ret);
  97. return ret;
  98. }
  99. EXPORT_SYMBOL_GPL(tc3589x_block_write);
  100. /**
  101. * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
  102. * @tc3589x: Device to write to
  103. * @reg: Register to write
  104. * @mask: Mask of bits to set
  105. * @values: Value to set
  106. */
  107. int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
  108. {
  109. int ret;
  110. mutex_lock(&tc3589x->lock);
  111. ret = tc3589x_reg_read(tc3589x, reg);
  112. if (ret < 0)
  113. goto out;
  114. ret &= ~mask;
  115. ret |= val;
  116. ret = tc3589x_reg_write(tc3589x, reg, ret);
  117. out:
  118. mutex_unlock(&tc3589x->lock);
  119. return ret;
  120. }
  121. EXPORT_SYMBOL_GPL(tc3589x_set_bits);
  122. static struct resource gpio_resources[] = {
  123. {
  124. .start = TC3589x_INT_GPIIRQ,
  125. .end = TC3589x_INT_GPIIRQ,
  126. .flags = IORESOURCE_IRQ,
  127. },
  128. };
  129. static struct resource keypad_resources[] = {
  130. {
  131. .start = TC3589x_INT_KBDIRQ,
  132. .end = TC3589x_INT_KBDIRQ,
  133. .flags = IORESOURCE_IRQ,
  134. },
  135. };
  136. static const struct mfd_cell tc3589x_dev_gpio[] = {
  137. {
  138. .name = "tc3589x-gpio",
  139. .num_resources = ARRAY_SIZE(gpio_resources),
  140. .resources = &gpio_resources[0],
  141. .of_compatible = "toshiba,tc3589x-gpio",
  142. },
  143. };
  144. static const struct mfd_cell tc3589x_dev_keypad[] = {
  145. {
  146. .name = "tc3589x-keypad",
  147. .num_resources = ARRAY_SIZE(keypad_resources),
  148. .resources = &keypad_resources[0],
  149. .of_compatible = "toshiba,tc3589x-keypad",
  150. },
  151. };
  152. static irqreturn_t tc3589x_irq(int irq, void *data)
  153. {
  154. struct tc3589x *tc3589x = data;
  155. int status;
  156. again:
  157. status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
  158. if (status < 0)
  159. return IRQ_NONE;
  160. while (status) {
  161. int bit = __ffs(status);
  162. int virq = irq_create_mapping(tc3589x->domain, bit);
  163. handle_nested_irq(virq);
  164. status &= ~(1 << bit);
  165. }
  166. /*
  167. * A dummy read or write (to any register) appears to be necessary to
  168. * have the last interrupt clear (for example, GPIO IC write) take
  169. * effect. In such a case, recheck for any interrupt which is still
  170. * pending.
  171. */
  172. status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
  173. if (status)
  174. goto again;
  175. return IRQ_HANDLED;
  176. }
  177. static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
  178. irq_hw_number_t hwirq)
  179. {
  180. struct tc3589x *tc3589x = d->host_data;
  181. irq_set_chip_data(virq, tc3589x);
  182. irq_set_chip_and_handler(virq, &dummy_irq_chip,
  183. handle_edge_irq);
  184. irq_set_nested_thread(virq, 1);
  185. irq_set_noprobe(virq);
  186. return 0;
  187. }
  188. static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
  189. {
  190. irq_set_chip_and_handler(virq, NULL, NULL);
  191. irq_set_chip_data(virq, NULL);
  192. }
  193. static const struct irq_domain_ops tc3589x_irq_ops = {
  194. .map = tc3589x_irq_map,
  195. .unmap = tc3589x_irq_unmap,
  196. .xlate = irq_domain_xlate_onecell,
  197. };
  198. static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
  199. {
  200. tc3589x->domain = irq_domain_add_simple(
  201. np, TC3589x_NR_INTERNAL_IRQS, 0,
  202. &tc3589x_irq_ops, tc3589x);
  203. if (!tc3589x->domain) {
  204. dev_err(tc3589x->dev, "Failed to create irqdomain\n");
  205. return -ENOSYS;
  206. }
  207. return 0;
  208. }
  209. static int tc3589x_chip_init(struct tc3589x *tc3589x)
  210. {
  211. int manf, ver, ret;
  212. manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
  213. if (manf < 0)
  214. return manf;
  215. ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
  216. if (ver < 0)
  217. return ver;
  218. if (manf != TC3589x_MANFCODE_MAGIC) {
  219. dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
  220. return -EINVAL;
  221. }
  222. dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
  223. /*
  224. * Put everything except the IRQ module into reset;
  225. * also spare the GPIO module for any pin initialization
  226. * done during pre-kernel boot
  227. */
  228. ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
  229. TC3589x_RSTCTRL_TIMRST
  230. | TC3589x_RSTCTRL_ROTRST
  231. | TC3589x_RSTCTRL_KBDRST);
  232. if (ret < 0)
  233. return ret;
  234. /* Clear the reset interrupt. */
  235. return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
  236. }
  237. static int tc3589x_device_init(struct tc3589x *tc3589x)
  238. {
  239. int ret = 0;
  240. unsigned int blocks = tc3589x->pdata->block;
  241. if (blocks & TC3589x_BLOCK_GPIO) {
  242. ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
  243. ARRAY_SIZE(tc3589x_dev_gpio), NULL,
  244. 0, tc3589x->domain);
  245. if (ret) {
  246. dev_err(tc3589x->dev, "failed to add gpio child\n");
  247. return ret;
  248. }
  249. dev_info(tc3589x->dev, "added gpio block\n");
  250. }
  251. if (blocks & TC3589x_BLOCK_KEYPAD) {
  252. ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
  253. ARRAY_SIZE(tc3589x_dev_keypad), NULL,
  254. 0, tc3589x->domain);
  255. if (ret) {
  256. dev_err(tc3589x->dev, "failed to keypad child\n");
  257. return ret;
  258. }
  259. dev_info(tc3589x->dev, "added keypad block\n");
  260. }
  261. return ret;
  262. }
  263. static const struct of_device_id tc3589x_match[] = {
  264. /* Legacy compatible string */
  265. { .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN },
  266. { .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 },
  267. { .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 },
  268. { .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 },
  269. { .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 },
  270. { .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 },
  271. { .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 },
  272. { }
  273. };
  274. MODULE_DEVICE_TABLE(of, tc3589x_match);
  275. static struct tc3589x_platform_data *
  276. tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
  277. {
  278. struct device_node *np = dev->of_node;
  279. struct tc3589x_platform_data *pdata;
  280. struct device_node *child;
  281. const struct of_device_id *of_id;
  282. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  283. if (!pdata)
  284. return ERR_PTR(-ENOMEM);
  285. of_id = of_match_device(tc3589x_match, dev);
  286. if (!of_id)
  287. return ERR_PTR(-ENODEV);
  288. *version = (enum tc3589x_version) of_id->data;
  289. for_each_child_of_node(np, child) {
  290. if (of_device_is_compatible(child, "toshiba,tc3589x-gpio"))
  291. pdata->block |= TC3589x_BLOCK_GPIO;
  292. if (of_device_is_compatible(child, "toshiba,tc3589x-keypad"))
  293. pdata->block |= TC3589x_BLOCK_KEYPAD;
  294. }
  295. return pdata;
  296. }
  297. static int tc3589x_probe(struct i2c_client *i2c,
  298. const struct i2c_device_id *id)
  299. {
  300. struct device_node *np = i2c->dev.of_node;
  301. struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
  302. struct tc3589x *tc3589x;
  303. enum tc3589x_version version;
  304. int ret;
  305. if (!pdata) {
  306. pdata = tc3589x_of_probe(&i2c->dev, &version);
  307. if (IS_ERR(pdata)) {
  308. dev_err(&i2c->dev, "No platform data or DT found\n");
  309. return PTR_ERR(pdata);
  310. }
  311. } else {
  312. /* When not probing from device tree we have this ID */
  313. version = id->driver_data;
  314. }
  315. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
  316. | I2C_FUNC_SMBUS_I2C_BLOCK))
  317. return -EIO;
  318. tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
  319. GFP_KERNEL);
  320. if (!tc3589x)
  321. return -ENOMEM;
  322. mutex_init(&tc3589x->lock);
  323. tc3589x->dev = &i2c->dev;
  324. tc3589x->i2c = i2c;
  325. tc3589x->pdata = pdata;
  326. switch (version) {
  327. case TC3589X_TC35893:
  328. case TC3589X_TC35895:
  329. case TC3589X_TC35896:
  330. tc3589x->num_gpio = 20;
  331. break;
  332. case TC3589X_TC35890:
  333. case TC3589X_TC35892:
  334. case TC3589X_TC35894:
  335. case TC3589X_UNKNOWN:
  336. default:
  337. tc3589x->num_gpio = 24;
  338. break;
  339. }
  340. i2c_set_clientdata(i2c, tc3589x);
  341. ret = tc3589x_chip_init(tc3589x);
  342. if (ret)
  343. return ret;
  344. ret = tc3589x_irq_init(tc3589x, np);
  345. if (ret)
  346. return ret;
  347. ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
  348. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  349. "tc3589x", tc3589x);
  350. if (ret) {
  351. dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
  352. return ret;
  353. }
  354. ret = tc3589x_device_init(tc3589x);
  355. if (ret) {
  356. dev_err(tc3589x->dev, "failed to add child devices\n");
  357. return ret;
  358. }
  359. return 0;
  360. }
  361. static int tc3589x_remove(struct i2c_client *client)
  362. {
  363. struct tc3589x *tc3589x = i2c_get_clientdata(client);
  364. mfd_remove_devices(tc3589x->dev);
  365. return 0;
  366. }
  367. #ifdef CONFIG_PM_SLEEP
  368. static int tc3589x_suspend(struct device *dev)
  369. {
  370. struct tc3589x *tc3589x = dev_get_drvdata(dev);
  371. struct i2c_client *client = tc3589x->i2c;
  372. int ret = 0;
  373. /* put the system to sleep mode */
  374. if (!device_may_wakeup(&client->dev))
  375. ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
  376. TC3589x_CLKMODE_MODCTL_SLEEP);
  377. return ret;
  378. }
  379. static int tc3589x_resume(struct device *dev)
  380. {
  381. struct tc3589x *tc3589x = dev_get_drvdata(dev);
  382. struct i2c_client *client = tc3589x->i2c;
  383. int ret = 0;
  384. /* enable the system into operation */
  385. if (!device_may_wakeup(&client->dev))
  386. ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
  387. TC3589x_CLKMODE_MODCTL_OPERATION);
  388. return ret;
  389. }
  390. #endif
  391. static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
  392. static const struct i2c_device_id tc3589x_id[] = {
  393. { "tc35890", TC3589X_TC35890 },
  394. { "tc35892", TC3589X_TC35892 },
  395. { "tc35893", TC3589X_TC35893 },
  396. { "tc35894", TC3589X_TC35894 },
  397. { "tc35895", TC3589X_TC35895 },
  398. { "tc35896", TC3589X_TC35896 },
  399. { "tc3589x", TC3589X_UNKNOWN },
  400. { }
  401. };
  402. MODULE_DEVICE_TABLE(i2c, tc3589x_id);
  403. static struct i2c_driver tc3589x_driver = {
  404. .driver = {
  405. .name = "tc3589x",
  406. .pm = &tc3589x_dev_pm_ops,
  407. .of_match_table = of_match_ptr(tc3589x_match),
  408. },
  409. .probe = tc3589x_probe,
  410. .remove = tc3589x_remove,
  411. .id_table = tc3589x_id,
  412. };
  413. static int __init tc3589x_init(void)
  414. {
  415. return i2c_add_driver(&tc3589x_driver);
  416. }
  417. subsys_initcall(tc3589x_init);
  418. static void __exit tc3589x_exit(void)
  419. {
  420. i2c_del_driver(&tc3589x_driver);
  421. }
  422. module_exit(tc3589x_exit);
  423. MODULE_LICENSE("GPL v2");
  424. MODULE_DESCRIPTION("TC3589x MFD core driver");
  425. MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");