gpio-adp5588.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * GPIO Chip driver for Analog Devices
  3. * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
  4. *
  5. * Copyright 2009-2010 Analog Devices Inc.
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/i2c.h>
  14. #include <linux/gpio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/i2c/adp5588.h>
  18. #define DRV_NAME "adp5588-gpio"
  19. /*
  20. * Early pre 4.0 Silicon required to delay readout by at least 25ms,
  21. * since the Event Counter Register updated 25ms after the interrupt
  22. * asserted.
  23. */
  24. #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
  25. struct adp5588_gpio {
  26. struct i2c_client *client;
  27. struct gpio_chip gpio_chip;
  28. struct mutex lock; /* protect cached dir, dat_out */
  29. /* protect serialized access to the interrupt controller bus */
  30. struct mutex irq_lock;
  31. unsigned gpio_start;
  32. unsigned irq_base;
  33. uint8_t dat_out[3];
  34. uint8_t dir[3];
  35. uint8_t int_lvl[3];
  36. uint8_t int_en[3];
  37. uint8_t irq_mask[3];
  38. uint8_t irq_stat[3];
  39. };
  40. static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
  41. {
  42. int ret = i2c_smbus_read_byte_data(client, reg);
  43. if (ret < 0)
  44. dev_err(&client->dev, "Read Error\n");
  45. return ret;
  46. }
  47. static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
  48. {
  49. int ret = i2c_smbus_write_byte_data(client, reg, val);
  50. if (ret < 0)
  51. dev_err(&client->dev, "Write Error\n");
  52. return ret;
  53. }
  54. static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
  55. {
  56. struct adp5588_gpio *dev =
  57. container_of(chip, struct adp5588_gpio, gpio_chip);
  58. return !!(adp5588_gpio_read(dev->client,
  59. GPIO_DAT_STAT1 + ADP5588_BANK(off)) & ADP5588_BIT(off));
  60. }
  61. static void adp5588_gpio_set_value(struct gpio_chip *chip,
  62. unsigned off, int val)
  63. {
  64. unsigned bank, bit;
  65. struct adp5588_gpio *dev =
  66. container_of(chip, struct adp5588_gpio, gpio_chip);
  67. bank = ADP5588_BANK(off);
  68. bit = ADP5588_BIT(off);
  69. mutex_lock(&dev->lock);
  70. if (val)
  71. dev->dat_out[bank] |= bit;
  72. else
  73. dev->dat_out[bank] &= ~bit;
  74. adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
  75. dev->dat_out[bank]);
  76. mutex_unlock(&dev->lock);
  77. }
  78. static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
  79. {
  80. int ret;
  81. unsigned bank;
  82. struct adp5588_gpio *dev =
  83. container_of(chip, struct adp5588_gpio, gpio_chip);
  84. bank = ADP5588_BANK(off);
  85. mutex_lock(&dev->lock);
  86. dev->dir[bank] &= ~ADP5588_BIT(off);
  87. ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
  88. mutex_unlock(&dev->lock);
  89. return ret;
  90. }
  91. static int adp5588_gpio_direction_output(struct gpio_chip *chip,
  92. unsigned off, int val)
  93. {
  94. int ret;
  95. unsigned bank, bit;
  96. struct adp5588_gpio *dev =
  97. container_of(chip, struct adp5588_gpio, gpio_chip);
  98. bank = ADP5588_BANK(off);
  99. bit = ADP5588_BIT(off);
  100. mutex_lock(&dev->lock);
  101. dev->dir[bank] |= bit;
  102. if (val)
  103. dev->dat_out[bank] |= bit;
  104. else
  105. dev->dat_out[bank] &= ~bit;
  106. ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
  107. dev->dat_out[bank]);
  108. ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
  109. dev->dir[bank]);
  110. mutex_unlock(&dev->lock);
  111. return ret;
  112. }
  113. #ifdef CONFIG_GPIO_ADP5588_IRQ
  114. static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
  115. {
  116. struct adp5588_gpio *dev =
  117. container_of(chip, struct adp5588_gpio, gpio_chip);
  118. return dev->irq_base + off;
  119. }
  120. static void adp5588_irq_bus_lock(struct irq_data *d)
  121. {
  122. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  123. mutex_lock(&dev->irq_lock);
  124. }
  125. /*
  126. * genirq core code can issue chip->mask/unmask from atomic context.
  127. * This doesn't work for slow busses where an access needs to sleep.
  128. * bus_sync_unlock() is therefore called outside the atomic context,
  129. * syncs the current irq mask state with the slow external controller
  130. * and unlocks the bus.
  131. */
  132. static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
  133. {
  134. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  135. int i;
  136. for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++)
  137. if (dev->int_en[i] ^ dev->irq_mask[i]) {
  138. dev->int_en[i] = dev->irq_mask[i];
  139. adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i,
  140. dev->int_en[i]);
  141. }
  142. mutex_unlock(&dev->irq_lock);
  143. }
  144. static void adp5588_irq_mask(struct irq_data *d)
  145. {
  146. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  147. unsigned gpio = d->irq - dev->irq_base;
  148. dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
  149. }
  150. static void adp5588_irq_unmask(struct irq_data *d)
  151. {
  152. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  153. unsigned gpio = d->irq - dev->irq_base;
  154. dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
  155. }
  156. static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
  157. {
  158. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  159. uint16_t gpio = d->irq - dev->irq_base;
  160. unsigned bank, bit;
  161. if ((type & IRQ_TYPE_EDGE_BOTH)) {
  162. dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
  163. d->irq, type);
  164. return -EINVAL;
  165. }
  166. bank = ADP5588_BANK(gpio);
  167. bit = ADP5588_BIT(gpio);
  168. if (type & IRQ_TYPE_LEVEL_HIGH)
  169. dev->int_lvl[bank] |= bit;
  170. else if (type & IRQ_TYPE_LEVEL_LOW)
  171. dev->int_lvl[bank] &= ~bit;
  172. else
  173. return -EINVAL;
  174. adp5588_gpio_direction_input(&dev->gpio_chip, gpio);
  175. adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank,
  176. dev->int_lvl[bank]);
  177. return 0;
  178. }
  179. static struct irq_chip adp5588_irq_chip = {
  180. .name = "adp5588",
  181. .irq_mask = adp5588_irq_mask,
  182. .irq_unmask = adp5588_irq_unmask,
  183. .irq_bus_lock = adp5588_irq_bus_lock,
  184. .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
  185. .irq_set_type = adp5588_irq_set_type,
  186. };
  187. static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
  188. {
  189. int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
  190. if (ret < 0)
  191. dev_err(&client->dev, "Read INT_STAT Error\n");
  192. return ret;
  193. }
  194. static irqreturn_t adp5588_irq_handler(int irq, void *devid)
  195. {
  196. struct adp5588_gpio *dev = devid;
  197. unsigned status, bank, bit, pending;
  198. int ret;
  199. status = adp5588_gpio_read(dev->client, INT_STAT);
  200. if (status & ADP5588_GPI_INT) {
  201. ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
  202. if (ret < 0)
  203. memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
  204. for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
  205. bank++, bit = 0) {
  206. pending = dev->irq_stat[bank] & dev->irq_mask[bank];
  207. while (pending) {
  208. if (pending & (1 << bit)) {
  209. handle_nested_irq(dev->irq_base +
  210. (bank << 3) + bit);
  211. pending &= ~(1 << bit);
  212. }
  213. bit++;
  214. }
  215. }
  216. }
  217. adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
  218. return IRQ_HANDLED;
  219. }
  220. static int adp5588_irq_setup(struct adp5588_gpio *dev)
  221. {
  222. struct i2c_client *client = dev->client;
  223. struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
  224. unsigned gpio;
  225. int ret;
  226. adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
  227. adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
  228. adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */
  229. dev->irq_base = pdata->irq_base;
  230. mutex_init(&dev->irq_lock);
  231. for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
  232. int irq = gpio + dev->irq_base;
  233. irq_set_chip_data(irq, dev);
  234. irq_set_chip_and_handler(irq, &adp5588_irq_chip,
  235. handle_level_irq);
  236. irq_set_nested_thread(irq, 1);
  237. #ifdef CONFIG_ARM
  238. /*
  239. * ARM needs us to explicitly flag the IRQ as VALID,
  240. * once we do so, it will also set the noprobe.
  241. */
  242. set_irq_flags(irq, IRQF_VALID);
  243. #else
  244. irq_set_noprobe(irq);
  245. #endif
  246. }
  247. ret = request_threaded_irq(client->irq,
  248. NULL,
  249. adp5588_irq_handler,
  250. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  251. dev_name(&client->dev), dev);
  252. if (ret) {
  253. dev_err(&client->dev, "failed to request irq %d\n",
  254. client->irq);
  255. goto out;
  256. }
  257. dev->gpio_chip.to_irq = adp5588_gpio_to_irq;
  258. adp5588_gpio_write(client, CFG,
  259. ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT);
  260. return 0;
  261. out:
  262. dev->irq_base = 0;
  263. return ret;
  264. }
  265. static void adp5588_irq_teardown(struct adp5588_gpio *dev)
  266. {
  267. if (dev->irq_base)
  268. free_irq(dev->client->irq, dev);
  269. }
  270. #else
  271. static int adp5588_irq_setup(struct adp5588_gpio *dev)
  272. {
  273. struct i2c_client *client = dev->client;
  274. dev_warn(&client->dev, "interrupt support not compiled in\n");
  275. return 0;
  276. }
  277. static void adp5588_irq_teardown(struct adp5588_gpio *dev)
  278. {
  279. }
  280. #endif /* CONFIG_GPIO_ADP5588_IRQ */
  281. static int __devinit adp5588_gpio_probe(struct i2c_client *client,
  282. const struct i2c_device_id *id)
  283. {
  284. struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
  285. struct adp5588_gpio *dev;
  286. struct gpio_chip *gc;
  287. int ret, i, revid;
  288. if (pdata == NULL) {
  289. dev_err(&client->dev, "missing platform data\n");
  290. return -ENODEV;
  291. }
  292. if (!i2c_check_functionality(client->adapter,
  293. I2C_FUNC_SMBUS_BYTE_DATA)) {
  294. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  295. return -EIO;
  296. }
  297. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  298. if (dev == NULL) {
  299. dev_err(&client->dev, "failed to alloc memory\n");
  300. return -ENOMEM;
  301. }
  302. dev->client = client;
  303. gc = &dev->gpio_chip;
  304. gc->direction_input = adp5588_gpio_direction_input;
  305. gc->direction_output = adp5588_gpio_direction_output;
  306. gc->get = adp5588_gpio_get_value;
  307. gc->set = adp5588_gpio_set_value;
  308. gc->can_sleep = 1;
  309. gc->base = pdata->gpio_start;
  310. gc->ngpio = ADP5588_MAXGPIO;
  311. gc->label = client->name;
  312. gc->owner = THIS_MODULE;
  313. mutex_init(&dev->lock);
  314. ret = adp5588_gpio_read(dev->client, DEV_ID);
  315. if (ret < 0)
  316. goto err;
  317. revid = ret & ADP5588_DEVICE_ID_MASK;
  318. for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  319. dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
  320. dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
  321. ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
  322. ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
  323. (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
  324. ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
  325. if (ret)
  326. goto err;
  327. }
  328. if (pdata->irq_base) {
  329. if (WA_DELAYED_READOUT_REVID(revid)) {
  330. dev_warn(&client->dev, "GPIO int not supported\n");
  331. } else {
  332. ret = adp5588_irq_setup(dev);
  333. if (ret)
  334. goto err;
  335. }
  336. }
  337. ret = gpiochip_add(&dev->gpio_chip);
  338. if (ret)
  339. goto err_irq;
  340. dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n",
  341. pdata->irq_base, revid);
  342. if (pdata->setup) {
  343. ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
  344. if (ret < 0)
  345. dev_warn(&client->dev, "setup failed, %d\n", ret);
  346. }
  347. i2c_set_clientdata(client, dev);
  348. return 0;
  349. err_irq:
  350. adp5588_irq_teardown(dev);
  351. err:
  352. kfree(dev);
  353. return ret;
  354. }
  355. static int __devexit adp5588_gpio_remove(struct i2c_client *client)
  356. {
  357. struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
  358. struct adp5588_gpio *dev = i2c_get_clientdata(client);
  359. int ret;
  360. if (pdata->teardown) {
  361. ret = pdata->teardown(client,
  362. dev->gpio_chip.base, dev->gpio_chip.ngpio,
  363. pdata->context);
  364. if (ret < 0) {
  365. dev_err(&client->dev, "teardown failed %d\n", ret);
  366. return ret;
  367. }
  368. }
  369. if (dev->irq_base)
  370. free_irq(dev->client->irq, dev);
  371. ret = gpiochip_remove(&dev->gpio_chip);
  372. if (ret) {
  373. dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
  374. return ret;
  375. }
  376. kfree(dev);
  377. return 0;
  378. }
  379. static const struct i2c_device_id adp5588_gpio_id[] = {
  380. {DRV_NAME, 0},
  381. {}
  382. };
  383. MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
  384. static struct i2c_driver adp5588_gpio_driver = {
  385. .driver = {
  386. .name = DRV_NAME,
  387. },
  388. .probe = adp5588_gpio_probe,
  389. .remove = __devexit_p(adp5588_gpio_remove),
  390. .id_table = adp5588_gpio_id,
  391. };
  392. static int __init adp5588_gpio_init(void)
  393. {
  394. return i2c_add_driver(&adp5588_gpio_driver);
  395. }
  396. module_init(adp5588_gpio_init);
  397. static void __exit adp5588_gpio_exit(void)
  398. {
  399. i2c_del_driver(&adp5588_gpio_driver);
  400. }
  401. module_exit(adp5588_gpio_exit);
  402. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  403. MODULE_DESCRIPTION("GPIO ADP5588 Driver");
  404. MODULE_LICENSE("GPL");