gpio-twl4030.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Access to GPIOs on TWL4030/TPS659x0 chips
  4. *
  5. * Copyright (C) 2006-2007 Texas Instruments, Inc.
  6. * Copyright (C) 2006 MontaVista Software, Inc.
  7. *
  8. * Code re-arranged and cleaned up by:
  9. * Syed Mohammed Khasim <x0khasim@ti.com>
  10. *
  11. * Initial Code:
  12. * Andy Lowe / Nishanth Menon
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kthread.h>
  18. #include <linux/irq.h>
  19. #include <linux/gpio/driver.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/of.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/mfd/twl.h>
  24. /*
  25. * The GPIO "subchip" supports 18 GPIOs which can be configured as
  26. * inputs or outputs, with pullups or pulldowns on each pin. Each
  27. * GPIO can trigger interrupts on either or both edges.
  28. *
  29. * GPIO interrupts can be fed to either of two IRQ lines; this is
  30. * intended to support multiple hosts.
  31. *
  32. * There are also two LED pins used sometimes as output-only GPIOs.
  33. */
  34. /* genirq interfaces are not available to modules */
  35. #ifdef MODULE
  36. #define is_module() true
  37. #else
  38. #define is_module() false
  39. #endif
  40. /* GPIO_CTRL Fields */
  41. #define MASK_GPIO_CTRL_GPIO0CD1 BIT(0)
  42. #define MASK_GPIO_CTRL_GPIO1CD2 BIT(1)
  43. #define MASK_GPIO_CTRL_GPIO_ON BIT(2)
  44. /* Mask for GPIO registers when aggregated into a 32-bit integer */
  45. #define GPIO_32_MASK 0x0003ffff
  46. struct gpio_twl4030_priv {
  47. struct gpio_chip gpio_chip;
  48. struct mutex mutex;
  49. int irq_base;
  50. /* Bitfields for state caching */
  51. unsigned int usage_count;
  52. unsigned int direction;
  53. unsigned int out_state;
  54. };
  55. /*----------------------------------------------------------------------*/
  56. /*
  57. * To configure TWL4030 GPIO module registers
  58. */
  59. static inline int gpio_twl4030_write(u8 address, u8 data)
  60. {
  61. return twl_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
  62. }
  63. /*----------------------------------------------------------------------*/
  64. /*
  65. * LED register offsets from TWL_MODULE_LED base
  66. * PWMs A and B are dedicated to LEDs A and B, respectively.
  67. */
  68. #define TWL4030_LED_LEDEN_REG 0x00
  69. #define TWL4030_PWMAON_REG 0x01
  70. #define TWL4030_PWMAOFF_REG 0x02
  71. #define TWL4030_PWMBON_REG 0x03
  72. #define TWL4030_PWMBOFF_REG 0x04
  73. /* LEDEN bits */
  74. #define LEDEN_LEDAON BIT(0)
  75. #define LEDEN_LEDBON BIT(1)
  76. #define LEDEN_LEDAEXT BIT(2)
  77. #define LEDEN_LEDBEXT BIT(3)
  78. #define LEDEN_LEDAPWM BIT(4)
  79. #define LEDEN_LEDBPWM BIT(5)
  80. #define LEDEN_PWM_LENGTHA BIT(6)
  81. #define LEDEN_PWM_LENGTHB BIT(7)
  82. #define PWMxON_LENGTH BIT(7)
  83. /*----------------------------------------------------------------------*/
  84. /*
  85. * To read a TWL4030 GPIO module register
  86. */
  87. static inline int gpio_twl4030_read(u8 address)
  88. {
  89. u8 data;
  90. int ret = 0;
  91. ret = twl_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
  92. return (ret < 0) ? ret : data;
  93. }
  94. /*----------------------------------------------------------------------*/
  95. static u8 cached_leden;
  96. /* The LED lines are open drain outputs ... a FET pulls to GND, so an
  97. * external pullup is needed. We could also expose the integrated PWM
  98. * as a LED brightness control; we initialize it as "always on".
  99. */
  100. static void twl4030_led_set_value(int led, int value)
  101. {
  102. u8 mask = LEDEN_LEDAON | LEDEN_LEDAPWM;
  103. if (led)
  104. mask <<= 1;
  105. if (value)
  106. cached_leden &= ~mask;
  107. else
  108. cached_leden |= mask;
  109. WARN_ON_ONCE(twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
  110. TWL4030_LED_LEDEN_REG));
  111. }
  112. static int twl4030_set_gpio_direction(int gpio, int is_input)
  113. {
  114. u8 d_bnk = gpio >> 3;
  115. u8 d_msk = BIT(gpio & 0x7);
  116. u8 reg = 0;
  117. u8 base = REG_GPIODATADIR1 + d_bnk;
  118. int ret = 0;
  119. ret = gpio_twl4030_read(base);
  120. if (ret >= 0) {
  121. if (is_input)
  122. reg = ret & ~d_msk;
  123. else
  124. reg = ret | d_msk;
  125. ret = gpio_twl4030_write(base, reg);
  126. }
  127. return ret;
  128. }
  129. static int twl4030_get_gpio_direction(int gpio)
  130. {
  131. u8 d_bnk = gpio >> 3;
  132. u8 d_msk = BIT(gpio & 0x7);
  133. u8 base = REG_GPIODATADIR1 + d_bnk;
  134. int ret = 0;
  135. ret = gpio_twl4030_read(base);
  136. if (ret < 0)
  137. return ret;
  138. if (ret & d_msk)
  139. return GPIO_LINE_DIRECTION_OUT;
  140. return GPIO_LINE_DIRECTION_IN;
  141. }
  142. static int twl4030_set_gpio_dataout(int gpio, int enable)
  143. {
  144. u8 d_bnk = gpio >> 3;
  145. u8 d_msk = BIT(gpio & 0x7);
  146. u8 base = 0;
  147. if (enable)
  148. base = REG_SETGPIODATAOUT1 + d_bnk;
  149. else
  150. base = REG_CLEARGPIODATAOUT1 + d_bnk;
  151. return gpio_twl4030_write(base, d_msk);
  152. }
  153. static int twl4030_get_gpio_datain(int gpio)
  154. {
  155. u8 d_bnk = gpio >> 3;
  156. u8 d_off = gpio & 0x7;
  157. u8 base = 0;
  158. int ret = 0;
  159. base = REG_GPIODATAIN1 + d_bnk;
  160. ret = gpio_twl4030_read(base);
  161. if (ret > 0)
  162. ret = (ret >> d_off) & 0x1;
  163. return ret;
  164. }
  165. /*----------------------------------------------------------------------*/
  166. static int twl_request(struct gpio_chip *chip, unsigned offset)
  167. {
  168. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  169. int status = 0;
  170. mutex_lock(&priv->mutex);
  171. /* Support the two LED outputs as output-only GPIOs. */
  172. if (offset >= TWL4030_GPIO_MAX) {
  173. u8 ledclr_mask = LEDEN_LEDAON | LEDEN_LEDAEXT
  174. | LEDEN_LEDAPWM | LEDEN_PWM_LENGTHA;
  175. u8 reg = TWL4030_PWMAON_REG;
  176. offset -= TWL4030_GPIO_MAX;
  177. if (offset) {
  178. ledclr_mask <<= 1;
  179. reg = TWL4030_PWMBON_REG;
  180. }
  181. /* initialize PWM to always-drive */
  182. /* Configure PWM OFF register first */
  183. status = twl_i2c_write_u8(TWL4030_MODULE_LED, 0x7f, reg + 1);
  184. if (status < 0)
  185. goto done;
  186. /* Followed by PWM ON register */
  187. status = twl_i2c_write_u8(TWL4030_MODULE_LED, 0x7f, reg);
  188. if (status < 0)
  189. goto done;
  190. /* init LED to not-driven (high) */
  191. status = twl_i2c_read_u8(TWL4030_MODULE_LED, &cached_leden,
  192. TWL4030_LED_LEDEN_REG);
  193. if (status < 0)
  194. goto done;
  195. cached_leden &= ~ledclr_mask;
  196. status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
  197. TWL4030_LED_LEDEN_REG);
  198. if (status < 0)
  199. goto done;
  200. status = 0;
  201. goto done;
  202. }
  203. /* on first use, turn GPIO module "on" */
  204. if (!priv->usage_count) {
  205. struct twl4030_gpio_platform_data *pdata;
  206. u8 value = MASK_GPIO_CTRL_GPIO_ON;
  207. /* optionally have the first two GPIOs switch vMMC1
  208. * and vMMC2 power supplies based on card presence.
  209. */
  210. pdata = dev_get_platdata(chip->parent);
  211. if (pdata)
  212. value |= pdata->mmc_cd & 0x03;
  213. status = gpio_twl4030_write(REG_GPIO_CTRL, value);
  214. }
  215. done:
  216. if (!status)
  217. priv->usage_count |= BIT(offset);
  218. mutex_unlock(&priv->mutex);
  219. return status;
  220. }
  221. static void twl_free(struct gpio_chip *chip, unsigned offset)
  222. {
  223. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  224. mutex_lock(&priv->mutex);
  225. if (offset >= TWL4030_GPIO_MAX) {
  226. twl4030_led_set_value(offset - TWL4030_GPIO_MAX, 1);
  227. goto out;
  228. }
  229. priv->usage_count &= ~BIT(offset);
  230. /* on last use, switch off GPIO module */
  231. if (!priv->usage_count)
  232. gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
  233. out:
  234. mutex_unlock(&priv->mutex);
  235. }
  236. static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
  237. {
  238. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  239. int ret;
  240. mutex_lock(&priv->mutex);
  241. if (offset < TWL4030_GPIO_MAX)
  242. ret = twl4030_set_gpio_direction(offset, 1);
  243. else
  244. ret = -EINVAL; /* LED outputs can't be set as input */
  245. if (!ret)
  246. priv->direction &= ~BIT(offset);
  247. mutex_unlock(&priv->mutex);
  248. return ret;
  249. }
  250. static int twl_get(struct gpio_chip *chip, unsigned offset)
  251. {
  252. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  253. int ret;
  254. int status = 0;
  255. mutex_lock(&priv->mutex);
  256. if (!(priv->usage_count & BIT(offset))) {
  257. ret = -EPERM;
  258. goto out;
  259. }
  260. if (priv->direction & BIT(offset))
  261. status = priv->out_state & BIT(offset);
  262. else
  263. status = twl4030_get_gpio_datain(offset);
  264. ret = (status < 0) ? status : !!status;
  265. out:
  266. mutex_unlock(&priv->mutex);
  267. return ret;
  268. }
  269. static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
  270. {
  271. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  272. mutex_lock(&priv->mutex);
  273. if (offset < TWL4030_GPIO_MAX)
  274. twl4030_set_gpio_dataout(offset, value);
  275. else
  276. twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
  277. if (value)
  278. priv->out_state |= BIT(offset);
  279. else
  280. priv->out_state &= ~BIT(offset);
  281. mutex_unlock(&priv->mutex);
  282. }
  283. static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
  284. {
  285. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  286. int ret = 0;
  287. mutex_lock(&priv->mutex);
  288. if (offset < TWL4030_GPIO_MAX) {
  289. ret = twl4030_set_gpio_direction(offset, 0);
  290. if (ret) {
  291. mutex_unlock(&priv->mutex);
  292. return ret;
  293. }
  294. }
  295. /*
  296. * LED gpios i.e. offset >= TWL4030_GPIO_MAX are always output
  297. */
  298. priv->direction |= BIT(offset);
  299. mutex_unlock(&priv->mutex);
  300. twl_set(chip, offset, value);
  301. return ret;
  302. }
  303. static int twl_get_direction(struct gpio_chip *chip, unsigned offset)
  304. {
  305. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  306. /*
  307. * Default GPIO_LINE_DIRECTION_OUT
  308. * LED GPIOs >= TWL4030_GPIO_MAX are always output
  309. */
  310. int ret = GPIO_LINE_DIRECTION_OUT;
  311. mutex_lock(&priv->mutex);
  312. if (offset < TWL4030_GPIO_MAX) {
  313. ret = twl4030_get_gpio_direction(offset);
  314. if (ret) {
  315. mutex_unlock(&priv->mutex);
  316. return ret;
  317. }
  318. }
  319. mutex_unlock(&priv->mutex);
  320. return ret;
  321. }
  322. static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
  323. {
  324. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  325. return (priv->irq_base && (offset < TWL4030_GPIO_MAX))
  326. ? (priv->irq_base + offset)
  327. : -EINVAL;
  328. }
  329. static const struct gpio_chip template_chip = {
  330. .label = "twl4030",
  331. .owner = THIS_MODULE,
  332. .request = twl_request,
  333. .free = twl_free,
  334. .direction_input = twl_direction_in,
  335. .direction_output = twl_direction_out,
  336. .get_direction = twl_get_direction,
  337. .get = twl_get,
  338. .set = twl_set,
  339. .to_irq = twl_to_irq,
  340. .can_sleep = true,
  341. };
  342. /*----------------------------------------------------------------------*/
  343. static int gpio_twl4030_pulls(u32 ups, u32 downs)
  344. {
  345. u8 message[5];
  346. unsigned i, gpio_bit;
  347. /* For most pins, a pulldown was enabled by default.
  348. * We should have data that's specific to this board.
  349. */
  350. for (gpio_bit = 1, i = 0; i < 5; i++) {
  351. u8 bit_mask;
  352. unsigned j;
  353. for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
  354. if (ups & gpio_bit)
  355. bit_mask |= 1 << (j + 1);
  356. else if (downs & gpio_bit)
  357. bit_mask |= 1 << (j + 0);
  358. }
  359. message[i] = bit_mask;
  360. }
  361. return twl_i2c_write(TWL4030_MODULE_GPIO, message,
  362. REG_GPIOPUPDCTR1, 5);
  363. }
  364. static int gpio_twl4030_debounce(u32 debounce, u8 mmc_cd)
  365. {
  366. u8 message[3];
  367. /* 30 msec of debouncing is always used for MMC card detect,
  368. * and is optional for everything else.
  369. */
  370. message[0] = (debounce & 0xff) | (mmc_cd & 0x03);
  371. debounce >>= 8;
  372. message[1] = (debounce & 0xff);
  373. debounce >>= 8;
  374. message[2] = (debounce & 0x03);
  375. return twl_i2c_write(TWL4030_MODULE_GPIO, message,
  376. REG_GPIO_DEBEN1, 3);
  377. }
  378. static int gpio_twl4030_remove(struct platform_device *pdev);
  379. static struct twl4030_gpio_platform_data *of_gpio_twl4030(struct device *dev,
  380. struct twl4030_gpio_platform_data *pdata)
  381. {
  382. struct twl4030_gpio_platform_data *omap_twl_info;
  383. omap_twl_info = devm_kzalloc(dev, sizeof(*omap_twl_info), GFP_KERNEL);
  384. if (!omap_twl_info)
  385. return NULL;
  386. if (pdata)
  387. *omap_twl_info = *pdata;
  388. omap_twl_info->use_leds = of_property_read_bool(dev->of_node,
  389. "ti,use-leds");
  390. of_property_read_u32(dev->of_node, "ti,debounce",
  391. &omap_twl_info->debounce);
  392. of_property_read_u32(dev->of_node, "ti,mmc-cd",
  393. (u32 *)&omap_twl_info->mmc_cd);
  394. of_property_read_u32(dev->of_node, "ti,pullups",
  395. &omap_twl_info->pullups);
  396. of_property_read_u32(dev->of_node, "ti,pulldowns",
  397. &omap_twl_info->pulldowns);
  398. return omap_twl_info;
  399. }
  400. static int gpio_twl4030_probe(struct platform_device *pdev)
  401. {
  402. struct twl4030_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  403. struct device_node *node = pdev->dev.of_node;
  404. struct gpio_twl4030_priv *priv;
  405. int ret, irq_base;
  406. priv = devm_kzalloc(&pdev->dev, sizeof(struct gpio_twl4030_priv),
  407. GFP_KERNEL);
  408. if (!priv)
  409. return -ENOMEM;
  410. /* maybe setup IRQs */
  411. if (is_module()) {
  412. dev_err(&pdev->dev, "can't dispatch IRQs from modules\n");
  413. goto no_irqs;
  414. }
  415. irq_base = devm_irq_alloc_descs(&pdev->dev, -1,
  416. 0, TWL4030_GPIO_MAX, 0);
  417. if (irq_base < 0) {
  418. dev_err(&pdev->dev, "Failed to alloc irq_descs\n");
  419. return irq_base;
  420. }
  421. irq_domain_add_legacy(node, TWL4030_GPIO_MAX, irq_base, 0,
  422. &irq_domain_simple_ops, NULL);
  423. ret = twl4030_sih_setup(&pdev->dev, TWL4030_MODULE_GPIO, irq_base);
  424. if (ret < 0)
  425. return ret;
  426. priv->irq_base = irq_base;
  427. no_irqs:
  428. priv->gpio_chip = template_chip;
  429. priv->gpio_chip.base = -1;
  430. priv->gpio_chip.ngpio = TWL4030_GPIO_MAX;
  431. priv->gpio_chip.parent = &pdev->dev;
  432. mutex_init(&priv->mutex);
  433. if (node)
  434. pdata = of_gpio_twl4030(&pdev->dev, pdata);
  435. if (pdata == NULL) {
  436. dev_err(&pdev->dev, "Platform data is missing\n");
  437. return -ENXIO;
  438. }
  439. /*
  440. * NOTE: boards may waste power if they don't set pullups
  441. * and pulldowns correctly ... default for non-ULPI pins is
  442. * pulldown, and some other pins may have external pullups
  443. * or pulldowns. Careful!
  444. */
  445. ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
  446. if (ret)
  447. dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
  448. pdata->pullups, pdata->pulldowns, ret);
  449. ret = gpio_twl4030_debounce(pdata->debounce, pdata->mmc_cd);
  450. if (ret)
  451. dev_dbg(&pdev->dev, "debounce %.03x %.01x --> %d\n",
  452. pdata->debounce, pdata->mmc_cd, ret);
  453. /*
  454. * NOTE: we assume VIBRA_CTL.VIBRA_EN, in MODULE_AUDIO_VOICE,
  455. * is (still) clear if use_leds is set.
  456. */
  457. if (pdata->use_leds)
  458. priv->gpio_chip.ngpio += 2;
  459. ret = gpiochip_add_data(&priv->gpio_chip, priv);
  460. if (ret < 0) {
  461. dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
  462. priv->gpio_chip.ngpio = 0;
  463. gpio_twl4030_remove(pdev);
  464. goto out;
  465. }
  466. platform_set_drvdata(pdev, priv);
  467. if (pdata->setup) {
  468. int status;
  469. status = pdata->setup(&pdev->dev, priv->gpio_chip.base,
  470. TWL4030_GPIO_MAX);
  471. if (status)
  472. dev_dbg(&pdev->dev, "setup --> %d\n", status);
  473. }
  474. out:
  475. return ret;
  476. }
  477. /* Cannot use as gpio_twl4030_probe() calls us */
  478. static int gpio_twl4030_remove(struct platform_device *pdev)
  479. {
  480. struct twl4030_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  481. struct gpio_twl4030_priv *priv = platform_get_drvdata(pdev);
  482. int status;
  483. if (pdata && pdata->teardown) {
  484. status = pdata->teardown(&pdev->dev, priv->gpio_chip.base,
  485. TWL4030_GPIO_MAX);
  486. if (status) {
  487. dev_dbg(&pdev->dev, "teardown --> %d\n", status);
  488. return status;
  489. }
  490. }
  491. gpiochip_remove(&priv->gpio_chip);
  492. if (is_module())
  493. return 0;
  494. /* REVISIT no support yet for deregistering all the IRQs */
  495. WARN_ON(1);
  496. return -EIO;
  497. }
  498. static const struct of_device_id twl_gpio_match[] = {
  499. { .compatible = "ti,twl4030-gpio", },
  500. { },
  501. };
  502. MODULE_DEVICE_TABLE(of, twl_gpio_match);
  503. /* Note: this hardware lives inside an I2C-based multi-function device. */
  504. MODULE_ALIAS("platform:twl4030_gpio");
  505. static struct platform_driver gpio_twl4030_driver = {
  506. .driver = {
  507. .name = "twl4030_gpio",
  508. .of_match_table = twl_gpio_match,
  509. },
  510. .probe = gpio_twl4030_probe,
  511. .remove = gpio_twl4030_remove,
  512. };
  513. static int __init gpio_twl4030_init(void)
  514. {
  515. return platform_driver_register(&gpio_twl4030_driver);
  516. }
  517. subsys_initcall(gpio_twl4030_init);
  518. static void __exit gpio_twl4030_exit(void)
  519. {
  520. platform_driver_unregister(&gpio_twl4030_driver);
  521. }
  522. module_exit(gpio_twl4030_exit);
  523. MODULE_AUTHOR("Texas Instruments, Inc.");
  524. MODULE_DESCRIPTION("GPIO interface for TWL4030");
  525. MODULE_LICENSE("GPL");