gpio-twl4030.c 15 KB

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