gpio-adnp.c 12 KB

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