gpio-adnp.c 12 KB

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