tc35892.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/tc35892.h>
  15. /**
  16. * tc35892_reg_read() - read a single TC35892 register
  17. * @tc35892: Device to read from
  18. * @reg: Register to read
  19. */
  20. int tc35892_reg_read(struct tc35892 *tc35892, u8 reg)
  21. {
  22. int ret;
  23. ret = i2c_smbus_read_byte_data(tc35892->i2c, reg);
  24. if (ret < 0)
  25. dev_err(tc35892->dev, "failed to read reg %#x: %d\n",
  26. reg, ret);
  27. return ret;
  28. }
  29. EXPORT_SYMBOL_GPL(tc35892_reg_read);
  30. /**
  31. * tc35892_reg_read() - write a single TC35892 register
  32. * @tc35892: Device to write to
  33. * @reg: Register to read
  34. * @data: Value to write
  35. */
  36. int tc35892_reg_write(struct tc35892 *tc35892, u8 reg, u8 data)
  37. {
  38. int ret;
  39. ret = i2c_smbus_write_byte_data(tc35892->i2c, reg, data);
  40. if (ret < 0)
  41. dev_err(tc35892->dev, "failed to write reg %#x: %d\n",
  42. reg, ret);
  43. return ret;
  44. }
  45. EXPORT_SYMBOL_GPL(tc35892_reg_write);
  46. /**
  47. * tc35892_block_read() - read multiple TC35892 registers
  48. * @tc35892: Device to read from
  49. * @reg: First register
  50. * @length: Number of registers
  51. * @values: Buffer to write to
  52. */
  53. int tc35892_block_read(struct tc35892 *tc35892, u8 reg, u8 length, u8 *values)
  54. {
  55. int ret;
  56. ret = i2c_smbus_read_i2c_block_data(tc35892->i2c, reg, length, values);
  57. if (ret < 0)
  58. dev_err(tc35892->dev, "failed to read regs %#x: %d\n",
  59. reg, ret);
  60. return ret;
  61. }
  62. EXPORT_SYMBOL_GPL(tc35892_block_read);
  63. /**
  64. * tc35892_block_write() - write multiple TC35892 registers
  65. * @tc35892: Device to write to
  66. * @reg: First register
  67. * @length: Number of registers
  68. * @values: Values to write
  69. */
  70. int tc35892_block_write(struct tc35892 *tc35892, u8 reg, u8 length,
  71. const u8 *values)
  72. {
  73. int ret;
  74. ret = i2c_smbus_write_i2c_block_data(tc35892->i2c, reg, length,
  75. values);
  76. if (ret < 0)
  77. dev_err(tc35892->dev, "failed to write regs %#x: %d\n",
  78. reg, ret);
  79. return ret;
  80. }
  81. EXPORT_SYMBOL_GPL(tc35892_block_write);
  82. /**
  83. * tc35892_set_bits() - set the value of a bitfield in a TC35892 register
  84. * @tc35892: Device to write to
  85. * @reg: Register to write
  86. * @mask: Mask of bits to set
  87. * @values: Value to set
  88. */
  89. int tc35892_set_bits(struct tc35892 *tc35892, u8 reg, u8 mask, u8 val)
  90. {
  91. int ret;
  92. mutex_lock(&tc35892->lock);
  93. ret = tc35892_reg_read(tc35892, reg);
  94. if (ret < 0)
  95. goto out;
  96. ret &= ~mask;
  97. ret |= val;
  98. ret = tc35892_reg_write(tc35892, reg, ret);
  99. out:
  100. mutex_unlock(&tc35892->lock);
  101. return ret;
  102. }
  103. EXPORT_SYMBOL_GPL(tc35892_set_bits);
  104. static struct resource gpio_resources[] = {
  105. {
  106. .start = TC35892_INT_GPIIRQ,
  107. .end = TC35892_INT_GPIIRQ,
  108. .flags = IORESOURCE_IRQ,
  109. },
  110. };
  111. static struct mfd_cell tc35892_devs[] = {
  112. {
  113. .name = "tc35892-gpio",
  114. .num_resources = ARRAY_SIZE(gpio_resources),
  115. .resources = &gpio_resources[0],
  116. },
  117. };
  118. static irqreturn_t tc35892_irq(int irq, void *data)
  119. {
  120. struct tc35892 *tc35892 = data;
  121. int status;
  122. status = tc35892_reg_read(tc35892, TC35892_IRQST);
  123. if (status < 0)
  124. return IRQ_NONE;
  125. while (status) {
  126. int bit = __ffs(status);
  127. handle_nested_irq(tc35892->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. tc35892_reg_read(tc35892, TC35892_IRQST);
  136. return IRQ_HANDLED;
  137. }
  138. static void tc35892_irq_dummy(unsigned int irq)
  139. {
  140. /* No mask/unmask at this level */
  141. }
  142. static struct irq_chip tc35892_irq_chip = {
  143. .name = "tc35892",
  144. .mask = tc35892_irq_dummy,
  145. .unmask = tc35892_irq_dummy,
  146. };
  147. static int tc35892_irq_init(struct tc35892 *tc35892)
  148. {
  149. int base = tc35892->irq_base;
  150. int irq;
  151. for (irq = base; irq < base + TC35892_NR_INTERNAL_IRQS; irq++) {
  152. set_irq_chip_data(irq, tc35892);
  153. set_irq_chip_and_handler(irq, &tc35892_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 tc35892_irq_remove(struct tc35892 *tc35892)
  165. {
  166. int base = tc35892->irq_base;
  167. int irq;
  168. for (irq = base; irq < base + TC35892_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 tc35892_chip_init(struct tc35892 *tc35892)
  177. {
  178. int manf, ver, ret;
  179. manf = tc35892_reg_read(tc35892, TC35892_MANFCODE);
  180. if (manf < 0)
  181. return manf;
  182. ver = tc35892_reg_read(tc35892, TC35892_VERSION);
  183. if (ver < 0)
  184. return ver;
  185. if (manf != TC35892_MANFCODE_MAGIC) {
  186. dev_err(tc35892->dev, "unknown manufacturer: %#x\n", manf);
  187. return -EINVAL;
  188. }
  189. dev_info(tc35892->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
  190. /* Put everything except the IRQ module into reset */
  191. ret = tc35892_reg_write(tc35892, TC35892_RSTCTRL,
  192. TC35892_RSTCTRL_TIMRST
  193. | TC35892_RSTCTRL_ROTRST
  194. | TC35892_RSTCTRL_KBDRST
  195. | TC35892_RSTCTRL_GPIRST);
  196. if (ret < 0)
  197. return ret;
  198. /* Clear the reset interrupt. */
  199. return tc35892_reg_write(tc35892, TC35892_RSTINTCLR, 0x1);
  200. }
  201. static int __devinit tc35892_probe(struct i2c_client *i2c,
  202. const struct i2c_device_id *id)
  203. {
  204. struct tc35892_platform_data *pdata = i2c->dev.platform_data;
  205. struct tc35892 *tc35892;
  206. int ret;
  207. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
  208. | I2C_FUNC_SMBUS_I2C_BLOCK))
  209. return -EIO;
  210. tc35892 = kzalloc(sizeof(struct tc35892), GFP_KERNEL);
  211. if (!tc35892)
  212. return -ENOMEM;
  213. mutex_init(&tc35892->lock);
  214. tc35892->dev = &i2c->dev;
  215. tc35892->i2c = i2c;
  216. tc35892->pdata = pdata;
  217. tc35892->irq_base = pdata->irq_base;
  218. tc35892->num_gpio = id->driver_data;
  219. i2c_set_clientdata(i2c, tc35892);
  220. ret = tc35892_chip_init(tc35892);
  221. if (ret)
  222. goto out_free;
  223. ret = tc35892_irq_init(tc35892);
  224. if (ret)
  225. goto out_free;
  226. ret = request_threaded_irq(tc35892->i2c->irq, NULL, tc35892_irq,
  227. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  228. "tc35892", tc35892);
  229. if (ret) {
  230. dev_err(tc35892->dev, "failed to request IRQ: %d\n", ret);
  231. goto out_removeirq;
  232. }
  233. ret = mfd_add_devices(tc35892->dev, -1, tc35892_devs,
  234. ARRAY_SIZE(tc35892_devs), NULL,
  235. tc35892->irq_base);
  236. if (ret) {
  237. dev_err(tc35892->dev, "failed to add children\n");
  238. goto out_freeirq;
  239. }
  240. return 0;
  241. out_freeirq:
  242. free_irq(tc35892->i2c->irq, tc35892);
  243. out_removeirq:
  244. tc35892_irq_remove(tc35892);
  245. out_free:
  246. kfree(tc35892);
  247. return ret;
  248. }
  249. static int __devexit tc35892_remove(struct i2c_client *client)
  250. {
  251. struct tc35892 *tc35892 = i2c_get_clientdata(client);
  252. mfd_remove_devices(tc35892->dev);
  253. free_irq(tc35892->i2c->irq, tc35892);
  254. tc35892_irq_remove(tc35892);
  255. kfree(tc35892);
  256. return 0;
  257. }
  258. static const struct i2c_device_id tc35892_id[] = {
  259. { "tc35892", 24 },
  260. { }
  261. };
  262. MODULE_DEVICE_TABLE(i2c, tc35892_id);
  263. static struct i2c_driver tc35892_driver = {
  264. .driver.name = "tc35892",
  265. .driver.owner = THIS_MODULE,
  266. .probe = tc35892_probe,
  267. .remove = __devexit_p(tc35892_remove),
  268. .id_table = tc35892_id,
  269. };
  270. static int __init tc35892_init(void)
  271. {
  272. return i2c_add_driver(&tc35892_driver);
  273. }
  274. subsys_initcall(tc35892_init);
  275. static void __exit tc35892_exit(void)
  276. {
  277. i2c_del_driver(&tc35892_driver);
  278. }
  279. module_exit(tc35892_exit);
  280. MODULE_LICENSE("GPL v2");
  281. MODULE_DESCRIPTION("TC35892 MFD core driver");
  282. MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");