gpio-adnp.c 12 KB

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