driver_gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Sonics Silicon Backplane
  3. * GPIO driver
  4. *
  5. * Copyright 2011, Broadcom Corporation
  6. * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
  7. *
  8. * Licensed under the GNU/GPL. See COPYING for details.
  9. */
  10. #include <linux/gpio.h>
  11. #include <linux/irq.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/export.h>
  15. #include <linux/ssb/ssb.h>
  16. #include "ssb_private.h"
  17. /**************************************************
  18. * Shared
  19. **************************************************/
  20. static struct ssb_bus *ssb_gpio_get_bus(struct gpio_chip *chip)
  21. {
  22. return container_of(chip, struct ssb_bus, gpio);
  23. }
  24. #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
  25. static int ssb_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  26. {
  27. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  28. if (bus->bustype == SSB_BUSTYPE_SSB)
  29. return irq_find_mapping(bus->irq_domain, gpio);
  30. else
  31. return -EINVAL;
  32. }
  33. #endif
  34. /**************************************************
  35. * ChipCommon
  36. **************************************************/
  37. static int ssb_gpio_chipco_get_value(struct gpio_chip *chip, unsigned gpio)
  38. {
  39. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  40. return !!ssb_chipco_gpio_in(&bus->chipco, 1 << gpio);
  41. }
  42. static void ssb_gpio_chipco_set_value(struct gpio_chip *chip, unsigned gpio,
  43. int value)
  44. {
  45. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  46. ssb_chipco_gpio_out(&bus->chipco, 1 << gpio, value ? 1 << gpio : 0);
  47. }
  48. static int ssb_gpio_chipco_direction_input(struct gpio_chip *chip,
  49. unsigned gpio)
  50. {
  51. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  52. ssb_chipco_gpio_outen(&bus->chipco, 1 << gpio, 0);
  53. return 0;
  54. }
  55. static int ssb_gpio_chipco_direction_output(struct gpio_chip *chip,
  56. unsigned gpio, int value)
  57. {
  58. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  59. ssb_chipco_gpio_outen(&bus->chipco, 1 << gpio, 1 << gpio);
  60. ssb_chipco_gpio_out(&bus->chipco, 1 << gpio, value ? 1 << gpio : 0);
  61. return 0;
  62. }
  63. static int ssb_gpio_chipco_request(struct gpio_chip *chip, unsigned gpio)
  64. {
  65. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  66. ssb_chipco_gpio_control(&bus->chipco, 1 << gpio, 0);
  67. /* clear pulldown */
  68. ssb_chipco_gpio_pulldown(&bus->chipco, 1 << gpio, 0);
  69. /* Set pullup */
  70. ssb_chipco_gpio_pullup(&bus->chipco, 1 << gpio, 1 << gpio);
  71. return 0;
  72. }
  73. static void ssb_gpio_chipco_free(struct gpio_chip *chip, unsigned gpio)
  74. {
  75. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  76. /* clear pullup */
  77. ssb_chipco_gpio_pullup(&bus->chipco, 1 << gpio, 0);
  78. }
  79. #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
  80. static void ssb_gpio_irq_chipco_mask(struct irq_data *d)
  81. {
  82. struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
  83. int gpio = irqd_to_hwirq(d);
  84. ssb_chipco_gpio_intmask(&bus->chipco, BIT(gpio), 0);
  85. }
  86. static void ssb_gpio_irq_chipco_unmask(struct irq_data *d)
  87. {
  88. struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
  89. int gpio = irqd_to_hwirq(d);
  90. u32 val = ssb_chipco_gpio_in(&bus->chipco, BIT(gpio));
  91. ssb_chipco_gpio_polarity(&bus->chipco, BIT(gpio), val);
  92. ssb_chipco_gpio_intmask(&bus->chipco, BIT(gpio), BIT(gpio));
  93. }
  94. static struct irq_chip ssb_gpio_irq_chipco_chip = {
  95. .name = "SSB-GPIO-CC",
  96. .irq_mask = ssb_gpio_irq_chipco_mask,
  97. .irq_unmask = ssb_gpio_irq_chipco_unmask,
  98. };
  99. static irqreturn_t ssb_gpio_irq_chipco_handler(int irq, void *dev_id)
  100. {
  101. struct ssb_bus *bus = dev_id;
  102. struct ssb_chipcommon *chipco = &bus->chipco;
  103. u32 val = chipco_read32(chipco, SSB_CHIPCO_GPIOIN);
  104. u32 mask = chipco_read32(chipco, SSB_CHIPCO_GPIOIRQ);
  105. u32 pol = chipco_read32(chipco, SSB_CHIPCO_GPIOPOL);
  106. unsigned long irqs = (val ^ pol) & mask;
  107. int gpio;
  108. if (!irqs)
  109. return IRQ_NONE;
  110. for_each_set_bit(gpio, &irqs, bus->gpio.ngpio)
  111. generic_handle_irq(ssb_gpio_to_irq(&bus->gpio, gpio));
  112. ssb_chipco_gpio_polarity(chipco, irqs, val & irqs);
  113. return IRQ_HANDLED;
  114. }
  115. static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus *bus)
  116. {
  117. struct ssb_chipcommon *chipco = &bus->chipco;
  118. struct gpio_chip *chip = &bus->gpio;
  119. int gpio, hwirq, err;
  120. if (bus->bustype != SSB_BUSTYPE_SSB)
  121. return 0;
  122. bus->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
  123. &irq_domain_simple_ops, chipco);
  124. if (!bus->irq_domain) {
  125. err = -ENODEV;
  126. goto err_irq_domain;
  127. }
  128. for (gpio = 0; gpio < chip->ngpio; gpio++) {
  129. int irq = irq_create_mapping(bus->irq_domain, gpio);
  130. irq_set_chip_data(irq, bus);
  131. irq_set_chip_and_handler(irq, &ssb_gpio_irq_chipco_chip,
  132. handle_simple_irq);
  133. }
  134. hwirq = ssb_mips_irq(bus->chipco.dev) + 2;
  135. err = request_irq(hwirq, ssb_gpio_irq_chipco_handler, IRQF_SHARED,
  136. "gpio", bus);
  137. if (err)
  138. goto err_req_irq;
  139. ssb_chipco_gpio_intmask(&bus->chipco, ~0, 0);
  140. chipco_set32(chipco, SSB_CHIPCO_IRQMASK, SSB_CHIPCO_IRQ_GPIO);
  141. return 0;
  142. err_req_irq:
  143. for (gpio = 0; gpio < chip->ngpio; gpio++) {
  144. int irq = irq_find_mapping(bus->irq_domain, gpio);
  145. irq_dispose_mapping(irq);
  146. }
  147. irq_domain_remove(bus->irq_domain);
  148. err_irq_domain:
  149. return err;
  150. }
  151. static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus *bus)
  152. {
  153. struct ssb_chipcommon *chipco = &bus->chipco;
  154. struct gpio_chip *chip = &bus->gpio;
  155. int gpio;
  156. if (bus->bustype != SSB_BUSTYPE_SSB)
  157. return;
  158. chipco_mask32(chipco, SSB_CHIPCO_IRQMASK, ~SSB_CHIPCO_IRQ_GPIO);
  159. free_irq(ssb_mips_irq(bus->chipco.dev) + 2, chipco);
  160. for (gpio = 0; gpio < chip->ngpio; gpio++) {
  161. int irq = irq_find_mapping(bus->irq_domain, gpio);
  162. irq_dispose_mapping(irq);
  163. }
  164. irq_domain_remove(bus->irq_domain);
  165. }
  166. #else
  167. static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus *bus)
  168. {
  169. return 0;
  170. }
  171. static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus *bus)
  172. {
  173. }
  174. #endif
  175. static int ssb_gpio_chipco_init(struct ssb_bus *bus)
  176. {
  177. struct gpio_chip *chip = &bus->gpio;
  178. int err;
  179. chip->label = "ssb_chipco_gpio";
  180. chip->owner = THIS_MODULE;
  181. chip->request = ssb_gpio_chipco_request;
  182. chip->free = ssb_gpio_chipco_free;
  183. chip->get = ssb_gpio_chipco_get_value;
  184. chip->set = ssb_gpio_chipco_set_value;
  185. chip->direction_input = ssb_gpio_chipco_direction_input;
  186. chip->direction_output = ssb_gpio_chipco_direction_output;
  187. #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
  188. chip->to_irq = ssb_gpio_to_irq;
  189. #endif
  190. chip->ngpio = 16;
  191. /* There is just one SoC in one device and its GPIO addresses should be
  192. * deterministic to address them more easily. The other buses could get
  193. * a random base number. */
  194. if (bus->bustype == SSB_BUSTYPE_SSB)
  195. chip->base = 0;
  196. else
  197. chip->base = -1;
  198. err = ssb_gpio_irq_chipco_domain_init(bus);
  199. if (err)
  200. return err;
  201. err = gpiochip_add(chip);
  202. if (err) {
  203. ssb_gpio_irq_chipco_domain_exit(bus);
  204. return err;
  205. }
  206. return 0;
  207. }
  208. /**************************************************
  209. * EXTIF
  210. **************************************************/
  211. #ifdef CONFIG_SSB_DRIVER_EXTIF
  212. static int ssb_gpio_extif_get_value(struct gpio_chip *chip, unsigned gpio)
  213. {
  214. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  215. return !!ssb_extif_gpio_in(&bus->extif, 1 << gpio);
  216. }
  217. static void ssb_gpio_extif_set_value(struct gpio_chip *chip, unsigned gpio,
  218. int value)
  219. {
  220. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  221. ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
  222. }
  223. static int ssb_gpio_extif_direction_input(struct gpio_chip *chip,
  224. unsigned gpio)
  225. {
  226. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  227. ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 0);
  228. return 0;
  229. }
  230. static int ssb_gpio_extif_direction_output(struct gpio_chip *chip,
  231. unsigned gpio, int value)
  232. {
  233. struct ssb_bus *bus = ssb_gpio_get_bus(chip);
  234. ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 1 << gpio);
  235. ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
  236. return 0;
  237. }
  238. #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
  239. static void ssb_gpio_irq_extif_mask(struct irq_data *d)
  240. {
  241. struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
  242. int gpio = irqd_to_hwirq(d);
  243. ssb_extif_gpio_intmask(&bus->extif, BIT(gpio), 0);
  244. }
  245. static void ssb_gpio_irq_extif_unmask(struct irq_data *d)
  246. {
  247. struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
  248. int gpio = irqd_to_hwirq(d);
  249. u32 val = ssb_extif_gpio_in(&bus->extif, BIT(gpio));
  250. ssb_extif_gpio_polarity(&bus->extif, BIT(gpio), val);
  251. ssb_extif_gpio_intmask(&bus->extif, BIT(gpio), BIT(gpio));
  252. }
  253. static struct irq_chip ssb_gpio_irq_extif_chip = {
  254. .name = "SSB-GPIO-EXTIF",
  255. .irq_mask = ssb_gpio_irq_extif_mask,
  256. .irq_unmask = ssb_gpio_irq_extif_unmask,
  257. };
  258. static irqreturn_t ssb_gpio_irq_extif_handler(int irq, void *dev_id)
  259. {
  260. struct ssb_bus *bus = dev_id;
  261. struct ssb_extif *extif = &bus->extif;
  262. u32 val = ssb_read32(extif->dev, SSB_EXTIF_GPIO_IN);
  263. u32 mask = ssb_read32(extif->dev, SSB_EXTIF_GPIO_INTMASK);
  264. u32 pol = ssb_read32(extif->dev, SSB_EXTIF_GPIO_INTPOL);
  265. unsigned long irqs = (val ^ pol) & mask;
  266. int gpio;
  267. if (!irqs)
  268. return IRQ_NONE;
  269. for_each_set_bit(gpio, &irqs, bus->gpio.ngpio)
  270. generic_handle_irq(ssb_gpio_to_irq(&bus->gpio, gpio));
  271. ssb_extif_gpio_polarity(extif, irqs, val & irqs);
  272. return IRQ_HANDLED;
  273. }
  274. static int ssb_gpio_irq_extif_domain_init(struct ssb_bus *bus)
  275. {
  276. struct ssb_extif *extif = &bus->extif;
  277. struct gpio_chip *chip = &bus->gpio;
  278. int gpio, hwirq, err;
  279. if (bus->bustype != SSB_BUSTYPE_SSB)
  280. return 0;
  281. bus->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
  282. &irq_domain_simple_ops, extif);
  283. if (!bus->irq_domain) {
  284. err = -ENODEV;
  285. goto err_irq_domain;
  286. }
  287. for (gpio = 0; gpio < chip->ngpio; gpio++) {
  288. int irq = irq_create_mapping(bus->irq_domain, gpio);
  289. irq_set_chip_data(irq, bus);
  290. irq_set_chip_and_handler(irq, &ssb_gpio_irq_extif_chip,
  291. handle_simple_irq);
  292. }
  293. hwirq = ssb_mips_irq(bus->extif.dev) + 2;
  294. err = request_irq(hwirq, ssb_gpio_irq_extif_handler, IRQF_SHARED,
  295. "gpio", bus);
  296. if (err)
  297. goto err_req_irq;
  298. ssb_extif_gpio_intmask(&bus->extif, ~0, 0);
  299. return 0;
  300. err_req_irq:
  301. for (gpio = 0; gpio < chip->ngpio; gpio++) {
  302. int irq = irq_find_mapping(bus->irq_domain, gpio);
  303. irq_dispose_mapping(irq);
  304. }
  305. irq_domain_remove(bus->irq_domain);
  306. err_irq_domain:
  307. return err;
  308. }
  309. static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus *bus)
  310. {
  311. struct ssb_extif *extif = &bus->extif;
  312. struct gpio_chip *chip = &bus->gpio;
  313. int gpio;
  314. if (bus->bustype != SSB_BUSTYPE_SSB)
  315. return;
  316. free_irq(ssb_mips_irq(bus->extif.dev) + 2, extif);
  317. for (gpio = 0; gpio < chip->ngpio; gpio++) {
  318. int irq = irq_find_mapping(bus->irq_domain, gpio);
  319. irq_dispose_mapping(irq);
  320. }
  321. irq_domain_remove(bus->irq_domain);
  322. }
  323. #else
  324. static int ssb_gpio_irq_extif_domain_init(struct ssb_bus *bus)
  325. {
  326. return 0;
  327. }
  328. static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus *bus)
  329. {
  330. }
  331. #endif
  332. static int ssb_gpio_extif_init(struct ssb_bus *bus)
  333. {
  334. struct gpio_chip *chip = &bus->gpio;
  335. int err;
  336. chip->label = "ssb_extif_gpio";
  337. chip->owner = THIS_MODULE;
  338. chip->get = ssb_gpio_extif_get_value;
  339. chip->set = ssb_gpio_extif_set_value;
  340. chip->direction_input = ssb_gpio_extif_direction_input;
  341. chip->direction_output = ssb_gpio_extif_direction_output;
  342. #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
  343. chip->to_irq = ssb_gpio_to_irq;
  344. #endif
  345. chip->ngpio = 5;
  346. /* There is just one SoC in one device and its GPIO addresses should be
  347. * deterministic to address them more easily. The other buses could get
  348. * a random base number. */
  349. if (bus->bustype == SSB_BUSTYPE_SSB)
  350. chip->base = 0;
  351. else
  352. chip->base = -1;
  353. err = ssb_gpio_irq_extif_domain_init(bus);
  354. if (err)
  355. return err;
  356. err = gpiochip_add(chip);
  357. if (err) {
  358. ssb_gpio_irq_extif_domain_exit(bus);
  359. return err;
  360. }
  361. return 0;
  362. }
  363. #else
  364. static int ssb_gpio_extif_init(struct ssb_bus *bus)
  365. {
  366. return -ENOTSUPP;
  367. }
  368. #endif
  369. /**************************************************
  370. * Init
  371. **************************************************/
  372. int ssb_gpio_init(struct ssb_bus *bus)
  373. {
  374. if (ssb_chipco_available(&bus->chipco))
  375. return ssb_gpio_chipco_init(bus);
  376. else if (ssb_extif_available(&bus->extif))
  377. return ssb_gpio_extif_init(bus);
  378. else
  379. SSB_WARN_ON(1);
  380. return -1;
  381. }
  382. int ssb_gpio_unregister(struct ssb_bus *bus)
  383. {
  384. if (ssb_chipco_available(&bus->chipco) ||
  385. ssb_extif_available(&bus->extif)) {
  386. gpiochip_remove(&bus->gpio);
  387. return 0;
  388. } else {
  389. SSB_WARN_ON(1);
  390. }
  391. return -1;
  392. }