gpio-adp5588.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 = gpiochip_get_data(chip);
  57. unsigned bank = ADP5588_BANK(off);
  58. unsigned bit = ADP5588_BIT(off);
  59. int val;
  60. mutex_lock(&dev->lock);
  61. if (dev->dir[bank] & bit)
  62. val = dev->dat_out[bank];
  63. else
  64. val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
  65. mutex_unlock(&dev->lock);
  66. return !!(val & bit);
  67. }
  68. static void adp5588_gpio_set_value(struct gpio_chip *chip,
  69. unsigned off, int val)
  70. {
  71. unsigned bank, bit;
  72. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  73. bank = ADP5588_BANK(off);
  74. bit = ADP5588_BIT(off);
  75. mutex_lock(&dev->lock);
  76. if (val)
  77. dev->dat_out[bank] |= bit;
  78. else
  79. dev->dat_out[bank] &= ~bit;
  80. adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
  81. dev->dat_out[bank]);
  82. mutex_unlock(&dev->lock);
  83. }
  84. static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
  85. {
  86. int ret;
  87. unsigned bank;
  88. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  89. bank = ADP5588_BANK(off);
  90. mutex_lock(&dev->lock);
  91. dev->dir[bank] &= ~ADP5588_BIT(off);
  92. ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
  93. mutex_unlock(&dev->lock);
  94. return ret;
  95. }
  96. static int adp5588_gpio_direction_output(struct gpio_chip *chip,
  97. unsigned off, int val)
  98. {
  99. int ret;
  100. unsigned bank, bit;
  101. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  102. bank = ADP5588_BANK(off);
  103. bit = ADP5588_BIT(off);
  104. mutex_lock(&dev->lock);
  105. dev->dir[bank] |= bit;
  106. if (val)
  107. dev->dat_out[bank] |= bit;
  108. else
  109. dev->dat_out[bank] &= ~bit;
  110. ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
  111. dev->dat_out[bank]);
  112. ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
  113. dev->dir[bank]);
  114. mutex_unlock(&dev->lock);
  115. return ret;
  116. }
  117. #ifdef CONFIG_GPIO_ADP5588_IRQ
  118. static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
  119. {
  120. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  121. return dev->irq_base + off;
  122. }
  123. static void adp5588_irq_bus_lock(struct irq_data *d)
  124. {
  125. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  126. mutex_lock(&dev->irq_lock);
  127. }
  128. /*
  129. * genirq core code can issue chip->mask/unmask from atomic context.
  130. * This doesn't work for slow busses where an access needs to sleep.
  131. * bus_sync_unlock() is therefore called outside the atomic context,
  132. * syncs the current irq mask state with the slow external controller
  133. * and unlocks the bus.
  134. */
  135. static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
  136. {
  137. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  138. int i;
  139. for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++)
  140. if (dev->int_en[i] ^ dev->irq_mask[i]) {
  141. dev->int_en[i] = dev->irq_mask[i];
  142. adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i,
  143. dev->int_en[i]);
  144. }
  145. mutex_unlock(&dev->irq_lock);
  146. }
  147. static void adp5588_irq_mask(struct irq_data *d)
  148. {
  149. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  150. unsigned gpio = d->irq - dev->irq_base;
  151. dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
  152. }
  153. static void adp5588_irq_unmask(struct irq_data *d)
  154. {
  155. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  156. unsigned gpio = d->irq - dev->irq_base;
  157. dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
  158. }
  159. static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
  160. {
  161. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  162. uint16_t gpio = d->irq - dev->irq_base;
  163. unsigned bank, bit;
  164. if ((type & IRQ_TYPE_EDGE_BOTH)) {
  165. dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
  166. d->irq, type);
  167. return -EINVAL;
  168. }
  169. bank = ADP5588_BANK(gpio);
  170. bit = ADP5588_BIT(gpio);
  171. if (type & IRQ_TYPE_LEVEL_HIGH)
  172. dev->int_lvl[bank] |= bit;
  173. else if (type & IRQ_TYPE_LEVEL_LOW)
  174. dev->int_lvl[bank] &= ~bit;
  175. else
  176. return -EINVAL;
  177. adp5588_gpio_direction_input(&dev->gpio_chip, gpio);
  178. adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank,
  179. dev->int_lvl[bank]);
  180. return 0;
  181. }
  182. static struct irq_chip adp5588_irq_chip = {
  183. .name = "adp5588",
  184. .irq_mask = adp5588_irq_mask,
  185. .irq_unmask = adp5588_irq_unmask,
  186. .irq_bus_lock = adp5588_irq_bus_lock,
  187. .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
  188. .irq_set_type = adp5588_irq_set_type,
  189. };
  190. static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
  191. {
  192. int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
  193. if (ret < 0)
  194. dev_err(&client->dev, "Read INT_STAT Error\n");
  195. return ret;
  196. }
  197. static irqreturn_t adp5588_irq_handler(int irq, void *devid)
  198. {
  199. struct adp5588_gpio *dev = devid;
  200. unsigned status, bank, bit, pending;
  201. int ret;
  202. status = adp5588_gpio_read(dev->client, INT_STAT);
  203. if (status & ADP5588_GPI_INT) {
  204. ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
  205. if (ret < 0)
  206. memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
  207. for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
  208. bank++, bit = 0) {
  209. pending = dev->irq_stat[bank] & dev->irq_mask[bank];
  210. while (pending) {
  211. if (pending & (1 << bit)) {
  212. handle_nested_irq(dev->irq_base +
  213. (bank << 3) + bit);
  214. pending &= ~(1 << bit);
  215. }
  216. bit++;
  217. }
  218. }
  219. }
  220. adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
  221. return IRQ_HANDLED;
  222. }
  223. static int adp5588_irq_setup(struct adp5588_gpio *dev)
  224. {
  225. struct i2c_client *client = dev->client;
  226. struct adp5588_gpio_platform_data *pdata =
  227. dev_get_platdata(&client->dev);
  228. unsigned gpio;
  229. int ret;
  230. adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
  231. adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
  232. adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */
  233. dev->irq_base = pdata->irq_base;
  234. mutex_init(&dev->irq_lock);
  235. for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
  236. int irq = gpio + dev->irq_base;
  237. irq_set_chip_data(irq, dev);
  238. irq_set_chip_and_handler(irq, &adp5588_irq_chip,
  239. handle_level_irq);
  240. irq_set_nested_thread(irq, 1);
  241. irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE);
  242. }
  243. ret = request_threaded_irq(client->irq,
  244. NULL,
  245. adp5588_irq_handler,
  246. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  247. dev_name(&client->dev), dev);
  248. if (ret) {
  249. dev_err(&client->dev, "failed to request irq %d\n",
  250. client->irq);
  251. goto out;
  252. }
  253. dev->gpio_chip.to_irq = adp5588_gpio_to_irq;
  254. adp5588_gpio_write(client, CFG,
  255. ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT);
  256. return 0;
  257. out:
  258. dev->irq_base = 0;
  259. return ret;
  260. }
  261. static void adp5588_irq_teardown(struct adp5588_gpio *dev)
  262. {
  263. if (dev->irq_base)
  264. free_irq(dev->client->irq, dev);
  265. }
  266. #else
  267. static int adp5588_irq_setup(struct adp5588_gpio *dev)
  268. {
  269. struct i2c_client *client = dev->client;
  270. dev_warn(&client->dev, "interrupt support not compiled in\n");
  271. return 0;
  272. }
  273. static void adp5588_irq_teardown(struct adp5588_gpio *dev)
  274. {
  275. }
  276. #endif /* CONFIG_GPIO_ADP5588_IRQ */
  277. static int adp5588_gpio_probe(struct i2c_client *client,
  278. const struct i2c_device_id *id)
  279. {
  280. struct adp5588_gpio_platform_data *pdata =
  281. dev_get_platdata(&client->dev);
  282. struct adp5588_gpio *dev;
  283. struct gpio_chip *gc;
  284. int ret, i, revid;
  285. if (!pdata) {
  286. dev_err(&client->dev, "missing platform data\n");
  287. return -ENODEV;
  288. }
  289. if (!i2c_check_functionality(client->adapter,
  290. I2C_FUNC_SMBUS_BYTE_DATA)) {
  291. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  292. return -EIO;
  293. }
  294. dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
  295. if (!dev)
  296. return -ENOMEM;
  297. dev->client = client;
  298. gc = &dev->gpio_chip;
  299. gc->direction_input = adp5588_gpio_direction_input;
  300. gc->direction_output = adp5588_gpio_direction_output;
  301. gc->get = adp5588_gpio_get_value;
  302. gc->set = adp5588_gpio_set_value;
  303. gc->can_sleep = true;
  304. gc->base = pdata->gpio_start;
  305. gc->ngpio = ADP5588_MAXGPIO;
  306. gc->label = client->name;
  307. gc->owner = THIS_MODULE;
  308. gc->names = pdata->names;
  309. mutex_init(&dev->lock);
  310. ret = adp5588_gpio_read(dev->client, DEV_ID);
  311. if (ret < 0)
  312. goto err;
  313. revid = ret & ADP5588_DEVICE_ID_MASK;
  314. for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  315. dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
  316. dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
  317. ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
  318. ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
  319. (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
  320. ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
  321. if (ret)
  322. goto err;
  323. }
  324. if (pdata->irq_base) {
  325. if (WA_DELAYED_READOUT_REVID(revid)) {
  326. dev_warn(&client->dev, "GPIO int not supported\n");
  327. } else {
  328. ret = adp5588_irq_setup(dev);
  329. if (ret)
  330. goto err;
  331. }
  332. }
  333. ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
  334. if (ret)
  335. goto err_irq;
  336. dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n",
  337. pdata->irq_base, revid);
  338. if (pdata->setup) {
  339. ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
  340. if (ret < 0)
  341. dev_warn(&client->dev, "setup failed, %d\n", ret);
  342. }
  343. i2c_set_clientdata(client, dev);
  344. return 0;
  345. err_irq:
  346. adp5588_irq_teardown(dev);
  347. err:
  348. return ret;
  349. }
  350. static int adp5588_gpio_remove(struct i2c_client *client)
  351. {
  352. struct adp5588_gpio_platform_data *pdata =
  353. dev_get_platdata(&client->dev);
  354. struct adp5588_gpio *dev = i2c_get_clientdata(client);
  355. int ret;
  356. if (pdata->teardown) {
  357. ret = pdata->teardown(client,
  358. dev->gpio_chip.base, dev->gpio_chip.ngpio,
  359. pdata->context);
  360. if (ret < 0) {
  361. dev_err(&client->dev, "teardown failed %d\n", ret);
  362. return ret;
  363. }
  364. }
  365. if (dev->irq_base)
  366. free_irq(dev->client->irq, dev);
  367. return 0;
  368. }
  369. static const struct i2c_device_id adp5588_gpio_id[] = {
  370. {DRV_NAME, 0},
  371. {}
  372. };
  373. MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
  374. static struct i2c_driver adp5588_gpio_driver = {
  375. .driver = {
  376. .name = DRV_NAME,
  377. },
  378. .probe = adp5588_gpio_probe,
  379. .remove = adp5588_gpio_remove,
  380. .id_table = adp5588_gpio_id,
  381. };
  382. module_i2c_driver(adp5588_gpio_driver);
  383. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  384. MODULE_DESCRIPTION("GPIO ADP5588 Driver");
  385. MODULE_LICENSE("GPL");