gpio-adnp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Copyright (C) 2011-2012 Avionic Design GmbH
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/gpio/driver.h>
  9. #include <linux/i2c.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift)
  16. #define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift)
  17. #define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift)
  18. #define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift)
  19. #define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift)
  20. struct adnp {
  21. struct i2c_client *client;
  22. struct gpio_chip gpio;
  23. unsigned int reg_shift;
  24. struct mutex i2c_lock;
  25. struct mutex irq_lock;
  26. u8 *irq_enable;
  27. u8 *irq_level;
  28. u8 *irq_rise;
  29. u8 *irq_fall;
  30. u8 *irq_high;
  31. u8 *irq_low;
  32. };
  33. static int adnp_read(struct adnp *adnp, unsigned offset, uint8_t *value)
  34. {
  35. int err;
  36. err = i2c_smbus_read_byte_data(adnp->client, offset);
  37. if (err < 0) {
  38. dev_err(adnp->gpio.parent, "%s failed: %d\n",
  39. "i2c_smbus_read_byte_data()", err);
  40. return err;
  41. }
  42. *value = err;
  43. return 0;
  44. }
  45. static int adnp_write(struct adnp *adnp, unsigned offset, uint8_t value)
  46. {
  47. int err;
  48. err = i2c_smbus_write_byte_data(adnp->client, offset, value);
  49. if (err < 0) {
  50. dev_err(adnp->gpio.parent, "%s failed: %d\n",
  51. "i2c_smbus_write_byte_data()", err);
  52. return err;
  53. }
  54. return 0;
  55. }
  56. static int adnp_gpio_get(struct gpio_chip *chip, unsigned offset)
  57. {
  58. struct adnp *adnp = gpiochip_get_data(chip);
  59. unsigned int reg = offset >> adnp->reg_shift;
  60. unsigned int pos = offset & 7;
  61. u8 value;
  62. int err;
  63. err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &value);
  64. if (err < 0)
  65. return err;
  66. return (value & BIT(pos)) ? 1 : 0;
  67. }
  68. static void __adnp_gpio_set(struct adnp *adnp, unsigned offset, int value)
  69. {
  70. unsigned int reg = offset >> adnp->reg_shift;
  71. unsigned int pos = offset & 7;
  72. int err;
  73. u8 val;
  74. err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &val);
  75. if (err < 0)
  76. return;
  77. if (value)
  78. val |= BIT(pos);
  79. else
  80. val &= ~BIT(pos);
  81. adnp_write(adnp, GPIO_PLR(adnp) + reg, val);
  82. }
  83. static void adnp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  84. {
  85. struct adnp *adnp = gpiochip_get_data(chip);
  86. mutex_lock(&adnp->i2c_lock);
  87. __adnp_gpio_set(adnp, offset, value);
  88. mutex_unlock(&adnp->i2c_lock);
  89. }
  90. static int adnp_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  91. {
  92. struct adnp *adnp = gpiochip_get_data(chip);
  93. unsigned int reg = offset >> adnp->reg_shift;
  94. unsigned int pos = offset & 7;
  95. u8 value;
  96. int err;
  97. mutex_lock(&adnp->i2c_lock);
  98. err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
  99. if (err < 0)
  100. goto out;
  101. value &= ~BIT(pos);
  102. err = adnp_write(adnp, GPIO_DDR(adnp) + reg, value);
  103. if (err < 0)
  104. goto out;
  105. err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
  106. if (err < 0)
  107. goto out;
  108. if (value & BIT(pos)) {
  109. err = -EPERM;
  110. goto out;
  111. }
  112. err = 0;
  113. out:
  114. mutex_unlock(&adnp->i2c_lock);
  115. return err;
  116. }
  117. static int adnp_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  118. int value)
  119. {
  120. struct adnp *adnp = gpiochip_get_data(chip);
  121. unsigned int reg = offset >> adnp->reg_shift;
  122. unsigned int pos = offset & 7;
  123. int err;
  124. u8 val;
  125. mutex_lock(&adnp->i2c_lock);
  126. err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val);
  127. if (err < 0)
  128. goto out;
  129. val |= BIT(pos);
  130. err = adnp_write(adnp, GPIO_DDR(adnp) + reg, val);
  131. if (err < 0)
  132. goto out;
  133. err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val);
  134. if (err < 0)
  135. goto out;
  136. if (!(val & BIT(pos))) {
  137. err = -EPERM;
  138. goto out;
  139. }
  140. __adnp_gpio_set(adnp, offset, value);
  141. err = 0;
  142. out:
  143. mutex_unlock(&adnp->i2c_lock);
  144. return err;
  145. }
  146. static void adnp_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  147. {
  148. struct adnp *adnp = gpiochip_get_data(chip);
  149. unsigned int num_regs = 1 << adnp->reg_shift, i, j;
  150. int err;
  151. for (i = 0; i < num_regs; i++) {
  152. u8 ddr, plr, ier, isr;
  153. mutex_lock(&adnp->i2c_lock);
  154. err = adnp_read(adnp, GPIO_DDR(adnp) + i, &ddr);
  155. if (err < 0)
  156. goto unlock;
  157. err = adnp_read(adnp, GPIO_PLR(adnp) + i, &plr);
  158. if (err < 0)
  159. goto unlock;
  160. err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
  161. if (err < 0)
  162. goto unlock;
  163. err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
  164. if (err < 0)
  165. goto unlock;
  166. mutex_unlock(&adnp->i2c_lock);
  167. for (j = 0; j < 8; j++) {
  168. unsigned int bit = (i << adnp->reg_shift) + j;
  169. const char *direction = "input ";
  170. const char *level = "low ";
  171. const char *interrupt = "disabled";
  172. const char *pending = "";
  173. if (ddr & BIT(j))
  174. direction = "output";
  175. if (plr & BIT(j))
  176. level = "high";
  177. if (ier & BIT(j))
  178. interrupt = "enabled ";
  179. if (isr & BIT(j))
  180. pending = "pending";
  181. seq_printf(s, "%2u: %s %s IRQ %s %s\n", bit,
  182. direction, level, interrupt, pending);
  183. }
  184. }
  185. return;
  186. unlock:
  187. mutex_unlock(&adnp->i2c_lock);
  188. }
  189. static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios)
  190. {
  191. struct gpio_chip *chip = &adnp->gpio;
  192. int err;
  193. adnp->reg_shift = get_count_order(num_gpios) - 3;
  194. chip->direction_input = adnp_gpio_direction_input;
  195. chip->direction_output = adnp_gpio_direction_output;
  196. chip->get = adnp_gpio_get;
  197. chip->set = adnp_gpio_set;
  198. chip->can_sleep = true;
  199. if (IS_ENABLED(CONFIG_DEBUG_FS))
  200. chip->dbg_show = adnp_gpio_dbg_show;
  201. chip->base = -1;
  202. chip->ngpio = num_gpios;
  203. chip->label = adnp->client->name;
  204. chip->parent = &adnp->client->dev;
  205. chip->of_node = chip->parent->of_node;
  206. chip->owner = THIS_MODULE;
  207. err = devm_gpiochip_add_data(&adnp->client->dev, chip, adnp);
  208. if (err)
  209. return err;
  210. return 0;
  211. }
  212. static irqreturn_t adnp_irq(int irq, void *data)
  213. {
  214. struct adnp *adnp = data;
  215. unsigned int num_regs, i;
  216. num_regs = 1 << adnp->reg_shift;
  217. for (i = 0; i < num_regs; i++) {
  218. unsigned int base = i << adnp->reg_shift, bit;
  219. u8 changed, level, isr, ier;
  220. unsigned long pending;
  221. int err;
  222. mutex_lock(&adnp->i2c_lock);
  223. err = adnp_read(adnp, GPIO_PLR(adnp) + i, &level);
  224. if (err < 0) {
  225. mutex_unlock(&adnp->i2c_lock);
  226. continue;
  227. }
  228. err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
  229. if (err < 0) {
  230. mutex_unlock(&adnp->i2c_lock);
  231. continue;
  232. }
  233. err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
  234. if (err < 0) {
  235. mutex_unlock(&adnp->i2c_lock);
  236. continue;
  237. }
  238. mutex_unlock(&adnp->i2c_lock);
  239. /* determine pins that changed levels */
  240. changed = level ^ adnp->irq_level[i];
  241. /* compute edge-triggered interrupts */
  242. pending = changed & ((adnp->irq_fall[i] & ~level) |
  243. (adnp->irq_rise[i] & level));
  244. /* add in level-triggered interrupts */
  245. pending |= (adnp->irq_high[i] & level) |
  246. (adnp->irq_low[i] & ~level);
  247. /* mask out non-pending and disabled interrupts */
  248. pending &= isr & ier;
  249. for_each_set_bit(bit, &pending, 8) {
  250. unsigned int child_irq;
  251. child_irq = irq_find_mapping(adnp->gpio.irq.domain,
  252. base + bit);
  253. handle_nested_irq(child_irq);
  254. }
  255. }
  256. return IRQ_HANDLED;
  257. }
  258. static void adnp_irq_mask(struct irq_data *d)
  259. {
  260. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  261. struct adnp *adnp = gpiochip_get_data(gc);
  262. unsigned int reg = d->hwirq >> adnp->reg_shift;
  263. unsigned int pos = d->hwirq & 7;
  264. adnp->irq_enable[reg] &= ~BIT(pos);
  265. }
  266. static void adnp_irq_unmask(struct irq_data *d)
  267. {
  268. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  269. struct adnp *adnp = gpiochip_get_data(gc);
  270. unsigned int reg = d->hwirq >> adnp->reg_shift;
  271. unsigned int pos = d->hwirq & 7;
  272. adnp->irq_enable[reg] |= BIT(pos);
  273. }
  274. static int adnp_irq_set_type(struct irq_data *d, unsigned int type)
  275. {
  276. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  277. struct adnp *adnp = gpiochip_get_data(gc);
  278. unsigned int reg = d->hwirq >> adnp->reg_shift;
  279. unsigned int pos = d->hwirq & 7;
  280. if (type & IRQ_TYPE_EDGE_RISING)
  281. adnp->irq_rise[reg] |= BIT(pos);
  282. else
  283. adnp->irq_rise[reg] &= ~BIT(pos);
  284. if (type & IRQ_TYPE_EDGE_FALLING)
  285. adnp->irq_fall[reg] |= BIT(pos);
  286. else
  287. adnp->irq_fall[reg] &= ~BIT(pos);
  288. if (type & IRQ_TYPE_LEVEL_HIGH)
  289. adnp->irq_high[reg] |= BIT(pos);
  290. else
  291. adnp->irq_high[reg] &= ~BIT(pos);
  292. if (type & IRQ_TYPE_LEVEL_LOW)
  293. adnp->irq_low[reg] |= BIT(pos);
  294. else
  295. adnp->irq_low[reg] &= ~BIT(pos);
  296. return 0;
  297. }
  298. static void adnp_irq_bus_lock(struct irq_data *d)
  299. {
  300. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  301. struct adnp *adnp = gpiochip_get_data(gc);
  302. mutex_lock(&adnp->irq_lock);
  303. }
  304. static void adnp_irq_bus_unlock(struct irq_data *d)
  305. {
  306. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  307. struct adnp *adnp = gpiochip_get_data(gc);
  308. unsigned int num_regs = 1 << adnp->reg_shift, i;
  309. mutex_lock(&adnp->i2c_lock);
  310. for (i = 0; i < num_regs; i++)
  311. adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]);
  312. mutex_unlock(&adnp->i2c_lock);
  313. mutex_unlock(&adnp->irq_lock);
  314. }
  315. static struct irq_chip adnp_irq_chip = {
  316. .name = "gpio-adnp",
  317. .irq_mask = adnp_irq_mask,
  318. .irq_unmask = adnp_irq_unmask,
  319. .irq_set_type = adnp_irq_set_type,
  320. .irq_bus_lock = adnp_irq_bus_lock,
  321. .irq_bus_sync_unlock = adnp_irq_bus_unlock,
  322. };
  323. static int adnp_irq_setup(struct adnp *adnp)
  324. {
  325. unsigned int num_regs = 1 << adnp->reg_shift, i;
  326. struct gpio_chip *chip = &adnp->gpio;
  327. int err;
  328. mutex_init(&adnp->irq_lock);
  329. /*
  330. * Allocate memory to keep track of the current level and trigger
  331. * modes of the interrupts. To avoid multiple allocations, a single
  332. * large buffer is allocated and pointers are setup to point at the
  333. * corresponding offsets. For consistency, the layout of the buffer
  334. * is chosen to match the register layout of the hardware in that
  335. * each segment contains the corresponding bits for all interrupts.
  336. */
  337. adnp->irq_enable = devm_kcalloc(chip->parent, num_regs, 6,
  338. GFP_KERNEL);
  339. if (!adnp->irq_enable)
  340. return -ENOMEM;
  341. adnp->irq_level = adnp->irq_enable + (num_regs * 1);
  342. adnp->irq_rise = adnp->irq_enable + (num_regs * 2);
  343. adnp->irq_fall = adnp->irq_enable + (num_regs * 3);
  344. adnp->irq_high = adnp->irq_enable + (num_regs * 4);
  345. adnp->irq_low = adnp->irq_enable + (num_regs * 5);
  346. for (i = 0; i < num_regs; i++) {
  347. /*
  348. * Read the initial level of all pins to allow the emulation
  349. * of edge triggered interrupts.
  350. */
  351. err = adnp_read(adnp, GPIO_PLR(adnp) + i, &adnp->irq_level[i]);
  352. if (err < 0)
  353. return err;
  354. /* disable all interrupts */
  355. err = adnp_write(adnp, GPIO_IER(adnp) + i, 0);
  356. if (err < 0)
  357. return err;
  358. adnp->irq_enable[i] = 0x00;
  359. }
  360. err = devm_request_threaded_irq(chip->parent, adnp->client->irq,
  361. NULL, adnp_irq,
  362. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  363. dev_name(chip->parent), adnp);
  364. if (err != 0) {
  365. dev_err(chip->parent, "can't request IRQ#%d: %d\n",
  366. adnp->client->irq, err);
  367. return err;
  368. }
  369. err = gpiochip_irqchip_add_nested(chip,
  370. &adnp_irq_chip,
  371. 0,
  372. handle_simple_irq,
  373. IRQ_TYPE_NONE);
  374. if (err) {
  375. dev_err(chip->parent,
  376. "could not connect irqchip to gpiochip\n");
  377. return err;
  378. }
  379. gpiochip_set_nested_irqchip(chip, &adnp_irq_chip, adnp->client->irq);
  380. return 0;
  381. }
  382. static int adnp_i2c_probe(struct i2c_client *client,
  383. const struct i2c_device_id *id)
  384. {
  385. struct device_node *np = client->dev.of_node;
  386. struct adnp *adnp;
  387. u32 num_gpios;
  388. int err;
  389. err = of_property_read_u32(np, "nr-gpios", &num_gpios);
  390. if (err < 0)
  391. return err;
  392. client->irq = irq_of_parse_and_map(np, 0);
  393. if (!client->irq)
  394. return -EPROBE_DEFER;
  395. adnp = devm_kzalloc(&client->dev, sizeof(*adnp), GFP_KERNEL);
  396. if (!adnp)
  397. return -ENOMEM;
  398. mutex_init(&adnp->i2c_lock);
  399. adnp->client = client;
  400. err = adnp_gpio_setup(adnp, num_gpios);
  401. if (err)
  402. return err;
  403. if (of_find_property(np, "interrupt-controller", NULL)) {
  404. err = adnp_irq_setup(adnp);
  405. if (err)
  406. return err;
  407. }
  408. i2c_set_clientdata(client, adnp);
  409. return 0;
  410. }
  411. static const struct i2c_device_id adnp_i2c_id[] = {
  412. { "gpio-adnp" },
  413. { },
  414. };
  415. MODULE_DEVICE_TABLE(i2c, adnp_i2c_id);
  416. static const struct of_device_id adnp_of_match[] = {
  417. { .compatible = "ad,gpio-adnp", },
  418. { },
  419. };
  420. MODULE_DEVICE_TABLE(of, adnp_of_match);
  421. static struct i2c_driver adnp_i2c_driver = {
  422. .driver = {
  423. .name = "gpio-adnp",
  424. .of_match_table = adnp_of_match,
  425. },
  426. .probe = adnp_i2c_probe,
  427. .id_table = adnp_i2c_id,
  428. };
  429. module_i2c_driver(adnp_i2c_driver);
  430. MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander");
  431. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  432. MODULE_LICENSE("GPL");