gpio-adnp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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.h>
  9. #include <linux/i2c.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/module.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/slab.h>
  16. #define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift)
  17. #define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift)
  18. #define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift)
  19. #define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift)
  20. #define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift)
  21. struct adnp {
  22. struct i2c_client *client;
  23. struct gpio_chip gpio;
  24. unsigned int reg_shift;
  25. struct mutex i2c_lock;
  26. struct irq_domain *domain;
  27. struct mutex irq_lock;
  28. u8 *irq_enable;
  29. u8 *irq_level;
  30. u8 *irq_rise;
  31. u8 *irq_fall;
  32. u8 *irq_high;
  33. u8 *irq_low;
  34. };
  35. static inline struct adnp *to_adnp(struct gpio_chip *chip)
  36. {
  37. return container_of(chip, struct adnp, gpio);
  38. }
  39. static int adnp_read(struct adnp *adnp, unsigned offset, uint8_t *value)
  40. {
  41. int err;
  42. err = i2c_smbus_read_byte_data(adnp->client, offset);
  43. if (err < 0) {
  44. dev_err(adnp->gpio.dev, "%s failed: %d\n",
  45. "i2c_smbus_read_byte_data()", err);
  46. return err;
  47. }
  48. *value = err;
  49. return 0;
  50. }
  51. static int adnp_write(struct adnp *adnp, unsigned offset, uint8_t value)
  52. {
  53. int err;
  54. err = i2c_smbus_write_byte_data(adnp->client, offset, value);
  55. if (err < 0) {
  56. dev_err(adnp->gpio.dev, "%s failed: %d\n",
  57. "i2c_smbus_write_byte_data()", err);
  58. return err;
  59. }
  60. return 0;
  61. }
  62. static int adnp_gpio_get(struct gpio_chip *chip, unsigned offset)
  63. {
  64. struct adnp *adnp = to_adnp(chip);
  65. unsigned int reg = offset >> adnp->reg_shift;
  66. unsigned int pos = offset & 7;
  67. u8 value;
  68. int err;
  69. err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &value);
  70. if (err < 0)
  71. return err;
  72. return (value & BIT(pos)) ? 1 : 0;
  73. }
  74. static void __adnp_gpio_set(struct adnp *adnp, unsigned offset, int value)
  75. {
  76. unsigned int reg = offset >> adnp->reg_shift;
  77. unsigned int pos = offset & 7;
  78. int err;
  79. u8 val;
  80. err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &val);
  81. if (err < 0)
  82. return;
  83. if (value)
  84. val |= BIT(pos);
  85. else
  86. val &= ~BIT(pos);
  87. adnp_write(adnp, GPIO_PLR(adnp) + reg, val);
  88. }
  89. static void adnp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  90. {
  91. struct adnp *adnp = to_adnp(chip);
  92. mutex_lock(&adnp->i2c_lock);
  93. __adnp_gpio_set(adnp, offset, value);
  94. mutex_unlock(&adnp->i2c_lock);
  95. }
  96. static int adnp_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  97. {
  98. struct adnp *adnp = to_adnp(chip);
  99. unsigned int reg = offset >> adnp->reg_shift;
  100. unsigned int pos = offset & 7;
  101. u8 value;
  102. int err;
  103. mutex_lock(&adnp->i2c_lock);
  104. err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
  105. if (err < 0)
  106. goto out;
  107. value &= ~BIT(pos);
  108. err = adnp_write(adnp, GPIO_DDR(adnp) + reg, value);
  109. if (err < 0)
  110. goto out;
  111. err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
  112. if (err < 0)
  113. goto out;
  114. if (err & BIT(pos))
  115. err = -EACCES;
  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. adnp->reg_shift = get_count_order(num_gpios) - 3;
  202. chip->direction_input = adnp_gpio_direction_input;
  203. chip->direction_output = adnp_gpio_direction_output;
  204. chip->get = adnp_gpio_get;
  205. chip->set = adnp_gpio_set;
  206. chip->can_sleep = 1;
  207. if (IS_ENABLED(CONFIG_DEBUG_FS))
  208. chip->dbg_show = adnp_gpio_dbg_show;
  209. chip->base = -1;
  210. chip->ngpio = num_gpios;
  211. chip->label = adnp->client->name;
  212. chip->dev = &adnp->client->dev;
  213. chip->of_node = chip->dev->of_node;
  214. chip->owner = THIS_MODULE;
  215. return 0;
  216. }
  217. static irqreturn_t adnp_irq(int irq, void *data)
  218. {
  219. struct adnp *adnp = data;
  220. unsigned int num_regs, i;
  221. num_regs = 1 << adnp->reg_shift;
  222. for (i = 0; i < num_regs; i++) {
  223. unsigned int base = i << adnp->reg_shift, bit;
  224. u8 changed, level, isr, ier;
  225. unsigned long pending;
  226. int err;
  227. mutex_lock(&adnp->i2c_lock);
  228. err = adnp_read(adnp, GPIO_PLR(adnp) + i, &level);
  229. if (err < 0) {
  230. mutex_unlock(&adnp->i2c_lock);
  231. continue;
  232. }
  233. err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
  234. if (err < 0) {
  235. mutex_unlock(&adnp->i2c_lock);
  236. continue;
  237. }
  238. err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
  239. if (err < 0) {
  240. mutex_unlock(&adnp->i2c_lock);
  241. continue;
  242. }
  243. mutex_unlock(&adnp->i2c_lock);
  244. /* determine pins that changed levels */
  245. changed = level ^ adnp->irq_level[i];
  246. /* compute edge-triggered interrupts */
  247. pending = changed & ((adnp->irq_fall[i] & ~level) |
  248. (adnp->irq_rise[i] & level));
  249. /* add in level-triggered interrupts */
  250. pending |= (adnp->irq_high[i] & level) |
  251. (adnp->irq_low[i] & ~level);
  252. /* mask out non-pending and disabled interrupts */
  253. pending &= isr & ier;
  254. for_each_set_bit(bit, &pending, 8) {
  255. unsigned int virq;
  256. virq = irq_find_mapping(adnp->domain, base + bit);
  257. handle_nested_irq(virq);
  258. }
  259. }
  260. return IRQ_HANDLED;
  261. }
  262. static int adnp_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  263. {
  264. struct adnp *adnp = to_adnp(chip);
  265. return irq_create_mapping(adnp->domain, offset);
  266. }
  267. static void adnp_irq_mask(struct irq_data *data)
  268. {
  269. struct adnp *adnp = irq_data_get_irq_chip_data(data);
  270. unsigned int reg = data->hwirq >> adnp->reg_shift;
  271. unsigned int pos = data->hwirq & 7;
  272. adnp->irq_enable[reg] &= ~BIT(pos);
  273. }
  274. static void adnp_irq_unmask(struct irq_data *data)
  275. {
  276. struct adnp *adnp = irq_data_get_irq_chip_data(data);
  277. unsigned int reg = data->hwirq >> adnp->reg_shift;
  278. unsigned int pos = data->hwirq & 7;
  279. adnp->irq_enable[reg] |= BIT(pos);
  280. }
  281. static int adnp_irq_set_type(struct irq_data *data, unsigned int type)
  282. {
  283. struct adnp *adnp = irq_data_get_irq_chip_data(data);
  284. unsigned int reg = data->hwirq >> adnp->reg_shift;
  285. unsigned int pos = data->hwirq & 7;
  286. if (type & IRQ_TYPE_EDGE_RISING)
  287. adnp->irq_rise[reg] |= BIT(pos);
  288. else
  289. adnp->irq_rise[reg] &= ~BIT(pos);
  290. if (type & IRQ_TYPE_EDGE_FALLING)
  291. adnp->irq_fall[reg] |= BIT(pos);
  292. else
  293. adnp->irq_fall[reg] &= ~BIT(pos);
  294. if (type & IRQ_TYPE_LEVEL_HIGH)
  295. adnp->irq_high[reg] |= BIT(pos);
  296. else
  297. adnp->irq_high[reg] &= ~BIT(pos);
  298. if (type & IRQ_TYPE_LEVEL_LOW)
  299. adnp->irq_low[reg] |= BIT(pos);
  300. else
  301. adnp->irq_low[reg] &= ~BIT(pos);
  302. return 0;
  303. }
  304. static void adnp_irq_bus_lock(struct irq_data *data)
  305. {
  306. struct adnp *adnp = irq_data_get_irq_chip_data(data);
  307. mutex_lock(&adnp->irq_lock);
  308. }
  309. static void adnp_irq_bus_unlock(struct irq_data *data)
  310. {
  311. struct adnp *adnp = irq_data_get_irq_chip_data(data);
  312. unsigned int num_regs = 1 << adnp->reg_shift, i;
  313. mutex_lock(&adnp->i2c_lock);
  314. for (i = 0; i < num_regs; i++)
  315. adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]);
  316. mutex_unlock(&adnp->i2c_lock);
  317. mutex_unlock(&adnp->irq_lock);
  318. }
  319. static struct irq_chip adnp_irq_chip = {
  320. .name = "gpio-adnp",
  321. .irq_mask = adnp_irq_mask,
  322. .irq_unmask = adnp_irq_unmask,
  323. .irq_set_type = adnp_irq_set_type,
  324. .irq_bus_lock = adnp_irq_bus_lock,
  325. .irq_bus_sync_unlock = adnp_irq_bus_unlock,
  326. };
  327. static int adnp_irq_map(struct irq_domain *domain, unsigned int irq,
  328. irq_hw_number_t hwirq)
  329. {
  330. irq_set_chip_data(irq, domain->host_data);
  331. irq_set_chip(irq, &adnp_irq_chip);
  332. irq_set_nested_thread(irq, true);
  333. #ifdef CONFIG_ARM
  334. set_irq_flags(irq, IRQF_VALID);
  335. #else
  336. irq_set_noprobe(irq);
  337. #endif
  338. return 0;
  339. }
  340. static const struct irq_domain_ops adnp_irq_domain_ops = {
  341. .map = adnp_irq_map,
  342. .xlate = irq_domain_xlate_twocell,
  343. };
  344. static int adnp_irq_setup(struct adnp *adnp)
  345. {
  346. unsigned int num_regs = 1 << adnp->reg_shift, i;
  347. struct gpio_chip *chip = &adnp->gpio;
  348. int err;
  349. mutex_init(&adnp->irq_lock);
  350. /*
  351. * Allocate memory to keep track of the current level and trigger
  352. * modes of the interrupts. To avoid multiple allocations, a single
  353. * large buffer is allocated and pointers are setup to point at the
  354. * corresponding offsets. For consistency, the layout of the buffer
  355. * is chosen to match the register layout of the hardware in that
  356. * each segment contains the corresponding bits for all interrupts.
  357. */
  358. adnp->irq_enable = devm_kzalloc(chip->dev, num_regs * 6, GFP_KERNEL);
  359. if (!adnp->irq_enable)
  360. return -ENOMEM;
  361. adnp->irq_level = adnp->irq_enable + (num_regs * 1);
  362. adnp->irq_rise = adnp->irq_enable + (num_regs * 2);
  363. adnp->irq_fall = adnp->irq_enable + (num_regs * 3);
  364. adnp->irq_high = adnp->irq_enable + (num_regs * 4);
  365. adnp->irq_low = adnp->irq_enable + (num_regs * 5);
  366. for (i = 0; i < num_regs; i++) {
  367. /*
  368. * Read the initial level of all pins to allow the emulation
  369. * of edge triggered interrupts.
  370. */
  371. err = adnp_read(adnp, GPIO_PLR(adnp) + i, &adnp->irq_level[i]);
  372. if (err < 0)
  373. return err;
  374. /* disable all interrupts */
  375. err = adnp_write(adnp, GPIO_IER(adnp) + i, 0);
  376. if (err < 0)
  377. return err;
  378. adnp->irq_enable[i] = 0x00;
  379. }
  380. adnp->domain = irq_domain_add_linear(chip->of_node, chip->ngpio,
  381. &adnp_irq_domain_ops, adnp);
  382. err = request_threaded_irq(adnp->client->irq, NULL, adnp_irq,
  383. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  384. dev_name(chip->dev), adnp);
  385. if (err != 0) {
  386. dev_err(chip->dev, "can't request IRQ#%d: %d\n",
  387. adnp->client->irq, err);
  388. goto error;
  389. }
  390. chip->to_irq = adnp_gpio_to_irq;
  391. return 0;
  392. error:
  393. irq_domain_remove(adnp->domain);
  394. return err;
  395. }
  396. static void adnp_irq_teardown(struct adnp *adnp)
  397. {
  398. unsigned int irq, i;
  399. free_irq(adnp->client->irq, adnp);
  400. for (i = 0; i < adnp->gpio.ngpio; i++) {
  401. irq = irq_find_mapping(adnp->domain, i);
  402. if (irq > 0)
  403. irq_dispose_mapping(irq);
  404. }
  405. irq_domain_remove(adnp->domain);
  406. }
  407. static __devinit int adnp_i2c_probe(struct i2c_client *client,
  408. const struct i2c_device_id *id)
  409. {
  410. struct device_node *np = client->dev.of_node;
  411. struct adnp *adnp;
  412. u32 num_gpios;
  413. int err;
  414. err = of_property_read_u32(np, "nr-gpios", &num_gpios);
  415. if (err < 0)
  416. return err;
  417. client->irq = irq_of_parse_and_map(np, 0);
  418. if (!client->irq)
  419. return -EPROBE_DEFER;
  420. adnp = devm_kzalloc(&client->dev, sizeof(*adnp), GFP_KERNEL);
  421. if (!adnp)
  422. return -ENOMEM;
  423. mutex_init(&adnp->i2c_lock);
  424. adnp->client = client;
  425. err = adnp_gpio_setup(adnp, num_gpios);
  426. if (err < 0)
  427. return err;
  428. if (of_find_property(np, "interrupt-controller", NULL)) {
  429. err = adnp_irq_setup(adnp);
  430. if (err < 0)
  431. goto teardown;
  432. }
  433. err = gpiochip_add(&adnp->gpio);
  434. if (err < 0)
  435. goto teardown;
  436. i2c_set_clientdata(client, adnp);
  437. return 0;
  438. teardown:
  439. if (of_find_property(np, "interrupt-controller", NULL))
  440. adnp_irq_teardown(adnp);
  441. return err;
  442. }
  443. static __devexit int adnp_i2c_remove(struct i2c_client *client)
  444. {
  445. struct adnp *adnp = i2c_get_clientdata(client);
  446. struct device_node *np = client->dev.of_node;
  447. int err;
  448. err = gpiochip_remove(&adnp->gpio);
  449. if (err < 0) {
  450. dev_err(&client->dev, "%s failed: %d\n", "gpiochip_remove()",
  451. err);
  452. return err;
  453. }
  454. if (of_find_property(np, "interrupt-controller", NULL))
  455. adnp_irq_teardown(adnp);
  456. return 0;
  457. }
  458. static const struct i2c_device_id adnp_i2c_id[] __devinitconst = {
  459. { "gpio-adnp" },
  460. { },
  461. };
  462. MODULE_DEVICE_TABLE(i2c, adnp_i2c_id);
  463. static const struct of_device_id adnp_of_match[] __devinitconst = {
  464. { .compatible = "ad,gpio-adnp", },
  465. { },
  466. };
  467. MODULE_DEVICE_TABLE(of, adnp_of_match);
  468. static struct i2c_driver adnp_i2c_driver = {
  469. .driver = {
  470. .name = "gpio-adnp",
  471. .owner = THIS_MODULE,
  472. .of_match_table = of_match_ptr(adnp_of_match),
  473. },
  474. .probe = adnp_i2c_probe,
  475. .remove = __devexit_p(adnp_i2c_remove),
  476. .id_table = adnp_i2c_id,
  477. };
  478. module_i2c_driver(adnp_i2c_driver);
  479. MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander");
  480. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  481. MODULE_LICENSE("GPL");