gpio-twl4030.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * Access to GPIOs on TWL4030/TPS659x0 chips
  3. *
  4. * Copyright (C) 2006-2007 Texas Instruments, Inc.
  5. * Copyright (C) 2006 MontaVista Software, Inc.
  6. *
  7. * Code re-arranged and cleaned up by:
  8. * Syed Mohammed Khasim <x0khasim@ti.com>
  9. *
  10. * Initial Code:
  11. * Andy Lowe / Nishanth Menon
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/kthread.h>
  31. #include <linux/irq.h>
  32. #include <linux/gpio.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/of.h>
  35. #include <linux/irqdomain.h>
  36. #include <linux/mfd/twl.h>
  37. /*
  38. * The GPIO "subchip" supports 18 GPIOs which can be configured as
  39. * inputs or outputs, with pullups or pulldowns on each pin. Each
  40. * GPIO can trigger interrupts on either or both edges.
  41. *
  42. * GPIO interrupts can be fed to either of two IRQ lines; this is
  43. * intended to support multiple hosts.
  44. *
  45. * There are also two LED pins used sometimes as output-only GPIOs.
  46. */
  47. /* genirq interfaces are not available to modules */
  48. #ifdef MODULE
  49. #define is_module() true
  50. #else
  51. #define is_module() false
  52. #endif
  53. /* GPIO_CTRL Fields */
  54. #define MASK_GPIO_CTRL_GPIO0CD1 BIT(0)
  55. #define MASK_GPIO_CTRL_GPIO1CD2 BIT(1)
  56. #define MASK_GPIO_CTRL_GPIO_ON BIT(2)
  57. /* Mask for GPIO registers when aggregated into a 32-bit integer */
  58. #define GPIO_32_MASK 0x0003ffff
  59. struct gpio_twl4030_priv {
  60. struct gpio_chip gpio_chip;
  61. struct mutex mutex;
  62. int irq_base;
  63. /* Bitfields for state caching */
  64. unsigned int usage_count;
  65. unsigned int direction;
  66. unsigned int out_state;
  67. };
  68. /*----------------------------------------------------------------------*/
  69. /*
  70. * To configure TWL4030 GPIO module registers
  71. */
  72. static inline int gpio_twl4030_write(u8 address, u8 data)
  73. {
  74. return twl_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
  75. }
  76. /*----------------------------------------------------------------------*/
  77. /*
  78. * LED register offsets from TWL_MODULE_LED base
  79. * PWMs A and B are dedicated to LEDs A and B, respectively.
  80. */
  81. #define TWL4030_LED_LEDEN_REG 0x00
  82. #define TWL4030_PWMAON_REG 0x01
  83. #define TWL4030_PWMAOFF_REG 0x02
  84. #define TWL4030_PWMBON_REG 0x03
  85. #define TWL4030_PWMBOFF_REG 0x04
  86. /* LEDEN bits */
  87. #define LEDEN_LEDAON BIT(0)
  88. #define LEDEN_LEDBON BIT(1)
  89. #define LEDEN_LEDAEXT BIT(2)
  90. #define LEDEN_LEDBEXT BIT(3)
  91. #define LEDEN_LEDAPWM BIT(4)
  92. #define LEDEN_LEDBPWM BIT(5)
  93. #define LEDEN_PWM_LENGTHA BIT(6)
  94. #define LEDEN_PWM_LENGTHB BIT(7)
  95. #define PWMxON_LENGTH BIT(7)
  96. /*----------------------------------------------------------------------*/
  97. /*
  98. * To read a TWL4030 GPIO module register
  99. */
  100. static inline int gpio_twl4030_read(u8 address)
  101. {
  102. u8 data;
  103. int ret = 0;
  104. ret = twl_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
  105. return (ret < 0) ? ret : data;
  106. }
  107. /*----------------------------------------------------------------------*/
  108. static u8 cached_leden;
  109. /* The LED lines are open drain outputs ... a FET pulls to GND, so an
  110. * external pullup is needed. We could also expose the integrated PWM
  111. * as a LED brightness control; we initialize it as "always on".
  112. */
  113. static void twl4030_led_set_value(int led, int value)
  114. {
  115. u8 mask = LEDEN_LEDAON | LEDEN_LEDAPWM;
  116. if (led)
  117. mask <<= 1;
  118. if (value)
  119. cached_leden &= ~mask;
  120. else
  121. cached_leden |= mask;
  122. WARN_ON_ONCE(twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
  123. TWL4030_LED_LEDEN_REG));
  124. }
  125. static int twl4030_set_gpio_direction(int gpio, int is_input)
  126. {
  127. u8 d_bnk = gpio >> 3;
  128. u8 d_msk = BIT(gpio & 0x7);
  129. u8 reg = 0;
  130. u8 base = REG_GPIODATADIR1 + d_bnk;
  131. int ret = 0;
  132. ret = gpio_twl4030_read(base);
  133. if (ret >= 0) {
  134. if (is_input)
  135. reg = ret & ~d_msk;
  136. else
  137. reg = ret | d_msk;
  138. ret = gpio_twl4030_write(base, reg);
  139. }
  140. return ret;
  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_to_irq(struct gpio_chip *chip, unsigned offset)
  304. {
  305. struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
  306. return (priv->irq_base && (offset < TWL4030_GPIO_MAX))
  307. ? (priv->irq_base + offset)
  308. : -EINVAL;
  309. }
  310. static const struct gpio_chip template_chip = {
  311. .label = "twl4030",
  312. .owner = THIS_MODULE,
  313. .request = twl_request,
  314. .free = twl_free,
  315. .direction_input = twl_direction_in,
  316. .get = twl_get,
  317. .direction_output = twl_direction_out,
  318. .set = twl_set,
  319. .to_irq = twl_to_irq,
  320. .can_sleep = true,
  321. };
  322. /*----------------------------------------------------------------------*/
  323. static int gpio_twl4030_pulls(u32 ups, u32 downs)
  324. {
  325. u8 message[5];
  326. unsigned i, gpio_bit;
  327. /* For most pins, a pulldown was enabled by default.
  328. * We should have data that's specific to this board.
  329. */
  330. for (gpio_bit = 1, i = 0; i < 5; i++) {
  331. u8 bit_mask;
  332. unsigned j;
  333. for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
  334. if (ups & gpio_bit)
  335. bit_mask |= 1 << (j + 1);
  336. else if (downs & gpio_bit)
  337. bit_mask |= 1 << (j + 0);
  338. }
  339. message[i] = bit_mask;
  340. }
  341. return twl_i2c_write(TWL4030_MODULE_GPIO, message,
  342. REG_GPIOPUPDCTR1, 5);
  343. }
  344. static int gpio_twl4030_debounce(u32 debounce, u8 mmc_cd)
  345. {
  346. u8 message[3];
  347. /* 30 msec of debouncing is always used for MMC card detect,
  348. * and is optional for everything else.
  349. */
  350. message[0] = (debounce & 0xff) | (mmc_cd & 0x03);
  351. debounce >>= 8;
  352. message[1] = (debounce & 0xff);
  353. debounce >>= 8;
  354. message[2] = (debounce & 0x03);
  355. return twl_i2c_write(TWL4030_MODULE_GPIO, message,
  356. REG_GPIO_DEBEN1, 3);
  357. }
  358. static int gpio_twl4030_remove(struct platform_device *pdev);
  359. static struct twl4030_gpio_platform_data *of_gpio_twl4030(struct device *dev,
  360. struct twl4030_gpio_platform_data *pdata)
  361. {
  362. struct twl4030_gpio_platform_data *omap_twl_info;
  363. omap_twl_info = devm_kzalloc(dev, sizeof(*omap_twl_info), GFP_KERNEL);
  364. if (!omap_twl_info)
  365. return NULL;
  366. if (pdata)
  367. *omap_twl_info = *pdata;
  368. omap_twl_info->use_leds = of_property_read_bool(dev->of_node,
  369. "ti,use-leds");
  370. of_property_read_u32(dev->of_node, "ti,debounce",
  371. &omap_twl_info->debounce);
  372. of_property_read_u32(dev->of_node, "ti,mmc-cd",
  373. (u32 *)&omap_twl_info->mmc_cd);
  374. of_property_read_u32(dev->of_node, "ti,pullups",
  375. &omap_twl_info->pullups);
  376. of_property_read_u32(dev->of_node, "ti,pulldowns",
  377. &omap_twl_info->pulldowns);
  378. return omap_twl_info;
  379. }
  380. static int gpio_twl4030_probe(struct platform_device *pdev)
  381. {
  382. struct twl4030_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  383. struct device_node *node = pdev->dev.of_node;
  384. struct gpio_twl4030_priv *priv;
  385. int ret, irq_base;
  386. priv = devm_kzalloc(&pdev->dev, sizeof(struct gpio_twl4030_priv),
  387. GFP_KERNEL);
  388. if (!priv)
  389. return -ENOMEM;
  390. /* maybe setup IRQs */
  391. if (is_module()) {
  392. dev_err(&pdev->dev, "can't dispatch IRQs from modules\n");
  393. goto no_irqs;
  394. }
  395. irq_base = devm_irq_alloc_descs(&pdev->dev, -1,
  396. 0, TWL4030_GPIO_MAX, 0);
  397. if (irq_base < 0) {
  398. dev_err(&pdev->dev, "Failed to alloc irq_descs\n");
  399. return irq_base;
  400. }
  401. irq_domain_add_legacy(node, TWL4030_GPIO_MAX, irq_base, 0,
  402. &irq_domain_simple_ops, NULL);
  403. ret = twl4030_sih_setup(&pdev->dev, TWL4030_MODULE_GPIO, irq_base);
  404. if (ret < 0)
  405. return ret;
  406. priv->irq_base = irq_base;
  407. no_irqs:
  408. priv->gpio_chip = template_chip;
  409. priv->gpio_chip.base = -1;
  410. priv->gpio_chip.ngpio = TWL4030_GPIO_MAX;
  411. priv->gpio_chip.parent = &pdev->dev;
  412. mutex_init(&priv->mutex);
  413. if (node)
  414. pdata = of_gpio_twl4030(&pdev->dev, pdata);
  415. if (pdata == NULL) {
  416. dev_err(&pdev->dev, "Platform data is missing\n");
  417. return -ENXIO;
  418. }
  419. /*
  420. * NOTE: boards may waste power if they don't set pullups
  421. * and pulldowns correctly ... default for non-ULPI pins is
  422. * pulldown, and some other pins may have external pullups
  423. * or pulldowns. Careful!
  424. */
  425. ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
  426. if (ret)
  427. dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
  428. pdata->pullups, pdata->pulldowns, ret);
  429. ret = gpio_twl4030_debounce(pdata->debounce, pdata->mmc_cd);
  430. if (ret)
  431. dev_dbg(&pdev->dev, "debounce %.03x %.01x --> %d\n",
  432. pdata->debounce, pdata->mmc_cd, ret);
  433. /*
  434. * NOTE: we assume VIBRA_CTL.VIBRA_EN, in MODULE_AUDIO_VOICE,
  435. * is (still) clear if use_leds is set.
  436. */
  437. if (pdata->use_leds)
  438. priv->gpio_chip.ngpio += 2;
  439. ret = gpiochip_add_data(&priv->gpio_chip, priv);
  440. if (ret < 0) {
  441. dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
  442. priv->gpio_chip.ngpio = 0;
  443. gpio_twl4030_remove(pdev);
  444. goto out;
  445. }
  446. platform_set_drvdata(pdev, priv);
  447. if (pdata->setup) {
  448. int status;
  449. status = pdata->setup(&pdev->dev, priv->gpio_chip.base,
  450. TWL4030_GPIO_MAX);
  451. if (status)
  452. dev_dbg(&pdev->dev, "setup --> %d\n", status);
  453. }
  454. out:
  455. return ret;
  456. }
  457. /* Cannot use as gpio_twl4030_probe() calls us */
  458. static int gpio_twl4030_remove(struct platform_device *pdev)
  459. {
  460. struct twl4030_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  461. struct gpio_twl4030_priv *priv = platform_get_drvdata(pdev);
  462. int status;
  463. if (pdata && pdata->teardown) {
  464. status = pdata->teardown(&pdev->dev, priv->gpio_chip.base,
  465. TWL4030_GPIO_MAX);
  466. if (status) {
  467. dev_dbg(&pdev->dev, "teardown --> %d\n", status);
  468. return status;
  469. }
  470. }
  471. gpiochip_remove(&priv->gpio_chip);
  472. if (is_module())
  473. return 0;
  474. /* REVISIT no support yet for deregistering all the IRQs */
  475. WARN_ON(1);
  476. return -EIO;
  477. }
  478. static const struct of_device_id twl_gpio_match[] = {
  479. { .compatible = "ti,twl4030-gpio", },
  480. { },
  481. };
  482. MODULE_DEVICE_TABLE(of, twl_gpio_match);
  483. /* Note: this hardware lives inside an I2C-based multi-function device. */
  484. MODULE_ALIAS("platform:twl4030_gpio");
  485. static struct platform_driver gpio_twl4030_driver = {
  486. .driver = {
  487. .name = "twl4030_gpio",
  488. .of_match_table = twl_gpio_match,
  489. },
  490. .probe = gpio_twl4030_probe,
  491. .remove = gpio_twl4030_remove,
  492. };
  493. static int __init gpio_twl4030_init(void)
  494. {
  495. return platform_driver_register(&gpio_twl4030_driver);
  496. }
  497. subsys_initcall(gpio_twl4030_init);
  498. static void __exit gpio_twl4030_exit(void)
  499. {
  500. platform_driver_unregister(&gpio_twl4030_driver);
  501. }
  502. module_exit(gpio_twl4030_exit);
  503. MODULE_AUTHOR("Texas Instruments, Inc.");
  504. MODULE_DESCRIPTION("GPIO interface for TWL4030");
  505. MODULE_LICENSE("GPL");