tc3589x.c 9.3 KB

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