tc3589x.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
  123. if (status < 0)
  124. return IRQ_NONE;
  125. while (status) {
  126. int bit = __ffs(status);
  127. handle_nested_irq(tc3589x->irq_base + bit);
  128. status &= ~(1 << bit);
  129. }
  130. /*
  131. * A dummy read or write (to any register) appears to be necessary to
  132. * have the last interrupt clear (for example, GPIO IC write) take
  133. * effect.
  134. */
  135. tc3589x_reg_read(tc3589x, TC3589x_IRQST);
  136. return IRQ_HANDLED;
  137. }
  138. static void tc3589x_irq_dummy(unsigned int irq)
  139. {
  140. /* No mask/unmask at this level */
  141. }
  142. static struct irq_chip tc3589x_irq_chip = {
  143. .name = "tc3589x",
  144. .mask = tc3589x_irq_dummy,
  145. .unmask = tc3589x_irq_dummy,
  146. };
  147. static int tc3589x_irq_init(struct tc3589x *tc3589x)
  148. {
  149. int base = tc3589x->irq_base;
  150. int irq;
  151. for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) {
  152. set_irq_chip_data(irq, tc3589x);
  153. set_irq_chip_and_handler(irq, &tc3589x_irq_chip,
  154. handle_edge_irq);
  155. set_irq_nested_thread(irq, 1);
  156. #ifdef CONFIG_ARM
  157. set_irq_flags(irq, IRQF_VALID);
  158. #else
  159. set_irq_noprobe(irq);
  160. #endif
  161. }
  162. return 0;
  163. }
  164. static void tc3589x_irq_remove(struct tc3589x *tc3589x)
  165. {
  166. int base = tc3589x->irq_base;
  167. int irq;
  168. for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) {
  169. #ifdef CONFIG_ARM
  170. set_irq_flags(irq, 0);
  171. #endif
  172. set_irq_chip_and_handler(irq, NULL, NULL);
  173. set_irq_chip_data(irq, NULL);
  174. }
  175. }
  176. static int tc3589x_chip_init(struct tc3589x *tc3589x)
  177. {
  178. int manf, ver, ret;
  179. manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
  180. if (manf < 0)
  181. return manf;
  182. ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
  183. if (ver < 0)
  184. return ver;
  185. if (manf != TC3589x_MANFCODE_MAGIC) {
  186. dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
  187. return -EINVAL;
  188. }
  189. dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
  190. /* Put everything except the IRQ module into reset */
  191. ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
  192. TC3589x_RSTCTRL_TIMRST
  193. | TC3589x_RSTCTRL_ROTRST
  194. | TC3589x_RSTCTRL_KBDRST
  195. | TC3589x_RSTCTRL_GPIRST);
  196. if (ret < 0)
  197. return ret;
  198. /* Clear the reset interrupt. */
  199. return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
  200. }
  201. static int __devinit tc3589x_device_init(struct tc3589x *tc3589x)
  202. {
  203. int ret = 0;
  204. unsigned int blocks = tc3589x->pdata->block;
  205. if (blocks & TC3589x_BLOCK_GPIO) {
  206. ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
  207. ARRAY_SIZE(tc3589x_dev_gpio), NULL,
  208. tc3589x->irq_base);
  209. if (ret) {
  210. dev_err(tc3589x->dev, "failed to add gpio child\n");
  211. return ret;
  212. }
  213. dev_info(tc3589x->dev, "added gpio block\n");
  214. }
  215. return ret;
  216. }
  217. static int __devinit tc3589x_probe(struct i2c_client *i2c,
  218. const struct i2c_device_id *id)
  219. {
  220. struct tc3589x_platform_data *pdata = i2c->dev.platform_data;
  221. struct tc3589x *tc3589x;
  222. int ret;
  223. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
  224. | I2C_FUNC_SMBUS_I2C_BLOCK))
  225. return -EIO;
  226. tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL);
  227. if (!tc3589x)
  228. return -ENOMEM;
  229. mutex_init(&tc3589x->lock);
  230. tc3589x->dev = &i2c->dev;
  231. tc3589x->i2c = i2c;
  232. tc3589x->pdata = pdata;
  233. tc3589x->irq_base = pdata->irq_base;
  234. tc3589x->num_gpio = id->driver_data;
  235. i2c_set_clientdata(i2c, tc3589x);
  236. ret = tc3589x_chip_init(tc3589x);
  237. if (ret)
  238. goto out_free;
  239. ret = tc3589x_irq_init(tc3589x);
  240. if (ret)
  241. goto out_free;
  242. ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
  243. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  244. "tc3589x", tc3589x);
  245. if (ret) {
  246. dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
  247. goto out_removeirq;
  248. }
  249. ret = tc3589x_device_init(tc3589x);
  250. if (ret) {
  251. dev_err(tc3589x->dev, "failed to add child devices\n");
  252. goto out_freeirq;
  253. }
  254. return 0;
  255. out_freeirq:
  256. free_irq(tc3589x->i2c->irq, tc3589x);
  257. out_removeirq:
  258. tc3589x_irq_remove(tc3589x);
  259. out_free:
  260. kfree(tc3589x);
  261. return ret;
  262. }
  263. static int __devexit tc3589x_remove(struct i2c_client *client)
  264. {
  265. struct tc3589x *tc3589x = i2c_get_clientdata(client);
  266. mfd_remove_devices(tc3589x->dev);
  267. free_irq(tc3589x->i2c->irq, tc3589x);
  268. tc3589x_irq_remove(tc3589x);
  269. kfree(tc3589x);
  270. return 0;
  271. }
  272. static const struct i2c_device_id tc3589x_id[] = {
  273. { "tc3589x", 24 },
  274. { }
  275. };
  276. MODULE_DEVICE_TABLE(i2c, tc3589x_id);
  277. static struct i2c_driver tc3589x_driver = {
  278. .driver.name = "tc3589x",
  279. .driver.owner = THIS_MODULE,
  280. .probe = tc3589x_probe,
  281. .remove = __devexit_p(tc3589x_remove),
  282. .id_table = tc3589x_id,
  283. };
  284. static int __init tc3589x_init(void)
  285. {
  286. return i2c_add_driver(&tc3589x_driver);
  287. }
  288. subsys_initcall(tc3589x_init);
  289. static void __exit tc3589x_exit(void)
  290. {
  291. i2c_del_driver(&tc3589x_driver);
  292. }
  293. module_exit(tc3589x_exit);
  294. MODULE_LICENSE("GPL v2");
  295. MODULE_DESCRIPTION("TC3589x MFD core driver");
  296. MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");