tc3589x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. #ifdef CONFIG_ARM
  186. set_irq_flags(virq, IRQF_VALID);
  187. #else
  188. irq_set_noprobe(virq);
  189. #endif
  190. return 0;
  191. }
  192. static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
  193. {
  194. #ifdef CONFIG_ARM
  195. set_irq_flags(virq, 0);
  196. #endif
  197. irq_set_chip_and_handler(virq, NULL, NULL);
  198. irq_set_chip_data(virq, NULL);
  199. }
  200. static const struct irq_domain_ops tc3589x_irq_ops = {
  201. .map = tc3589x_irq_map,
  202. .unmap = tc3589x_irq_unmap,
  203. .xlate = irq_domain_xlate_onecell,
  204. };
  205. static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
  206. {
  207. tc3589x->domain = irq_domain_add_simple(
  208. np, TC3589x_NR_INTERNAL_IRQS, 0,
  209. &tc3589x_irq_ops, tc3589x);
  210. if (!tc3589x->domain) {
  211. dev_err(tc3589x->dev, "Failed to create irqdomain\n");
  212. return -ENOSYS;
  213. }
  214. return 0;
  215. }
  216. static int tc3589x_chip_init(struct tc3589x *tc3589x)
  217. {
  218. int manf, ver, ret;
  219. manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
  220. if (manf < 0)
  221. return manf;
  222. ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
  223. if (ver < 0)
  224. return ver;
  225. if (manf != TC3589x_MANFCODE_MAGIC) {
  226. dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
  227. return -EINVAL;
  228. }
  229. dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
  230. /*
  231. * Put everything except the IRQ module into reset;
  232. * also spare the GPIO module for any pin initialization
  233. * done during pre-kernel boot
  234. */
  235. ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
  236. TC3589x_RSTCTRL_TIMRST
  237. | TC3589x_RSTCTRL_ROTRST
  238. | TC3589x_RSTCTRL_KBDRST);
  239. if (ret < 0)
  240. return ret;
  241. /* Clear the reset interrupt. */
  242. return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
  243. }
  244. static int tc3589x_device_init(struct tc3589x *tc3589x)
  245. {
  246. int ret = 0;
  247. unsigned int blocks = tc3589x->pdata->block;
  248. if (blocks & TC3589x_BLOCK_GPIO) {
  249. ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
  250. ARRAY_SIZE(tc3589x_dev_gpio), NULL,
  251. 0, tc3589x->domain);
  252. if (ret) {
  253. dev_err(tc3589x->dev, "failed to add gpio child\n");
  254. return ret;
  255. }
  256. dev_info(tc3589x->dev, "added gpio block\n");
  257. }
  258. if (blocks & TC3589x_BLOCK_KEYPAD) {
  259. ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
  260. ARRAY_SIZE(tc3589x_dev_keypad), NULL,
  261. 0, tc3589x->domain);
  262. if (ret) {
  263. dev_err(tc3589x->dev, "failed to keypad child\n");
  264. return ret;
  265. }
  266. dev_info(tc3589x->dev, "added keypad block\n");
  267. }
  268. return ret;
  269. }
  270. static const struct of_device_id tc3589x_match[] = {
  271. /* Legacy compatible string */
  272. { .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN },
  273. { .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 },
  274. { .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 },
  275. { .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 },
  276. { .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 },
  277. { .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 },
  278. { .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 },
  279. { }
  280. };
  281. MODULE_DEVICE_TABLE(of, tc3589x_match);
  282. static struct tc3589x_platform_data *
  283. tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
  284. {
  285. struct device_node *np = dev->of_node;
  286. struct tc3589x_platform_data *pdata;
  287. struct device_node *child;
  288. const struct of_device_id *of_id;
  289. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  290. if (!pdata)
  291. return ERR_PTR(-ENOMEM);
  292. of_id = of_match_device(tc3589x_match, dev);
  293. if (!of_id)
  294. return ERR_PTR(-ENODEV);
  295. *version = (enum tc3589x_version) of_id->data;
  296. for_each_child_of_node(np, child) {
  297. if (of_device_is_compatible(child, "toshiba,tc3589x-gpio"))
  298. pdata->block |= TC3589x_BLOCK_GPIO;
  299. if (of_device_is_compatible(child, "toshiba,tc3589x-keypad"))
  300. pdata->block |= TC3589x_BLOCK_KEYPAD;
  301. }
  302. return pdata;
  303. }
  304. static int tc3589x_probe(struct i2c_client *i2c,
  305. const struct i2c_device_id *id)
  306. {
  307. struct device_node *np = i2c->dev.of_node;
  308. struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
  309. struct tc3589x *tc3589x;
  310. enum tc3589x_version version;
  311. int ret;
  312. if (!pdata) {
  313. pdata = tc3589x_of_probe(&i2c->dev, &version);
  314. if (IS_ERR(pdata)) {
  315. dev_err(&i2c->dev, "No platform data or DT found\n");
  316. return PTR_ERR(pdata);
  317. }
  318. } else {
  319. /* When not probing from device tree we have this ID */
  320. version = id->driver_data;
  321. }
  322. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
  323. | I2C_FUNC_SMBUS_I2C_BLOCK))
  324. return -EIO;
  325. tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
  326. GFP_KERNEL);
  327. if (!tc3589x)
  328. return -ENOMEM;
  329. mutex_init(&tc3589x->lock);
  330. tc3589x->dev = &i2c->dev;
  331. tc3589x->i2c = i2c;
  332. tc3589x->pdata = pdata;
  333. switch (version) {
  334. case TC3589X_TC35893:
  335. case TC3589X_TC35895:
  336. case TC3589X_TC35896:
  337. tc3589x->num_gpio = 20;
  338. break;
  339. case TC3589X_TC35890:
  340. case TC3589X_TC35892:
  341. case TC3589X_TC35894:
  342. case TC3589X_UNKNOWN:
  343. default:
  344. tc3589x->num_gpio = 24;
  345. break;
  346. }
  347. i2c_set_clientdata(i2c, tc3589x);
  348. ret = tc3589x_chip_init(tc3589x);
  349. if (ret)
  350. return ret;
  351. ret = tc3589x_irq_init(tc3589x, np);
  352. if (ret)
  353. return ret;
  354. ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
  355. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  356. "tc3589x", tc3589x);
  357. if (ret) {
  358. dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
  359. return ret;
  360. }
  361. ret = tc3589x_device_init(tc3589x);
  362. if (ret) {
  363. dev_err(tc3589x->dev, "failed to add child devices\n");
  364. return ret;
  365. }
  366. return 0;
  367. }
  368. static int tc3589x_remove(struct i2c_client *client)
  369. {
  370. struct tc3589x *tc3589x = i2c_get_clientdata(client);
  371. mfd_remove_devices(tc3589x->dev);
  372. return 0;
  373. }
  374. #ifdef CONFIG_PM_SLEEP
  375. static int tc3589x_suspend(struct device *dev)
  376. {
  377. struct tc3589x *tc3589x = dev_get_drvdata(dev);
  378. struct i2c_client *client = tc3589x->i2c;
  379. int ret = 0;
  380. /* put the system to sleep mode */
  381. if (!device_may_wakeup(&client->dev))
  382. ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
  383. TC3589x_CLKMODE_MODCTL_SLEEP);
  384. return ret;
  385. }
  386. static int tc3589x_resume(struct device *dev)
  387. {
  388. struct tc3589x *tc3589x = dev_get_drvdata(dev);
  389. struct i2c_client *client = tc3589x->i2c;
  390. int ret = 0;
  391. /* enable the system into operation */
  392. if (!device_may_wakeup(&client->dev))
  393. ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
  394. TC3589x_CLKMODE_MODCTL_OPERATION);
  395. return ret;
  396. }
  397. #endif
  398. static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
  399. static const struct i2c_device_id tc3589x_id[] = {
  400. { "tc35890", TC3589X_TC35890 },
  401. { "tc35892", TC3589X_TC35892 },
  402. { "tc35893", TC3589X_TC35893 },
  403. { "tc35894", TC3589X_TC35894 },
  404. { "tc35895", TC3589X_TC35895 },
  405. { "tc35896", TC3589X_TC35896 },
  406. { "tc3589x", TC3589X_UNKNOWN },
  407. { }
  408. };
  409. MODULE_DEVICE_TABLE(i2c, tc3589x_id);
  410. static struct i2c_driver tc3589x_driver = {
  411. .driver = {
  412. .name = "tc3589x",
  413. .owner = THIS_MODULE,
  414. .pm = &tc3589x_dev_pm_ops,
  415. .of_match_table = of_match_ptr(tc3589x_match),
  416. },
  417. .probe = tc3589x_probe,
  418. .remove = tc3589x_remove,
  419. .id_table = tc3589x_id,
  420. };
  421. static int __init tc3589x_init(void)
  422. {
  423. return i2c_add_driver(&tc3589x_driver);
  424. }
  425. subsys_initcall(tc3589x_init);
  426. static void __exit tc3589x_exit(void)
  427. {
  428. i2c_del_driver(&tc3589x_driver);
  429. }
  430. module_exit(tc3589x_exit);
  431. MODULE_LICENSE("GPL v2");
  432. MODULE_DESCRIPTION("TC3589x MFD core driver");
  433. MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");