gpio-adnp.c 12 KB

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