tc3589x.c 7.9 KB

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