tc3589x.c 11 KB

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