gpio-twl4030.c 15 KB

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