gpio-twl4030.c 15 KB

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