gpio-twl4030.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. static struct gpio_chip twl_gpiochip;
  48. static int twl4030_gpio_irq_base;
  49. /* genirq interfaces are not available to modules */
  50. #ifdef MODULE
  51. #define is_module() true
  52. #else
  53. #define is_module() false
  54. #endif
  55. /* GPIO_CTRL Fields */
  56. #define MASK_GPIO_CTRL_GPIO0CD1 BIT(0)
  57. #define MASK_GPIO_CTRL_GPIO1CD2 BIT(1)
  58. #define MASK_GPIO_CTRL_GPIO_ON BIT(2)
  59. /* Mask for GPIO registers when aggregated into a 32-bit integer */
  60. #define GPIO_32_MASK 0x0003ffff
  61. /* Data structures */
  62. static DEFINE_MUTEX(gpio_lock);
  63. /* store usage of each GPIO. - each bit represents one GPIO */
  64. static unsigned int gpio_usage_count;
  65. /*----------------------------------------------------------------------*/
  66. /*
  67. * To configure TWL4030 GPIO module registers
  68. */
  69. static inline int gpio_twl4030_write(u8 address, u8 data)
  70. {
  71. return twl_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
  72. }
  73. /*----------------------------------------------------------------------*/
  74. /*
  75. * LED register offsets (use TWL4030_MODULE_{LED,PWMA,PWMB}))
  76. * PWMs A and B are dedicated to LEDs A and B, respectively.
  77. */
  78. #define TWL4030_LED_LEDEN 0x0
  79. /* LEDEN bits */
  80. #define LEDEN_LEDAON BIT(0)
  81. #define LEDEN_LEDBON BIT(1)
  82. #define LEDEN_LEDAEXT BIT(2)
  83. #define LEDEN_LEDBEXT BIT(3)
  84. #define LEDEN_LEDAPWM BIT(4)
  85. #define LEDEN_LEDBPWM BIT(5)
  86. #define LEDEN_PWM_LENGTHA BIT(6)
  87. #define LEDEN_PWM_LENGTHB BIT(7)
  88. #define TWL4030_PWMx_PWMxON 0x0
  89. #define TWL4030_PWMx_PWMxOFF 0x1
  90. #define PWMxON_LENGTH BIT(7)
  91. /*----------------------------------------------------------------------*/
  92. /*
  93. * To read a TWL4030 GPIO module register
  94. */
  95. static inline int gpio_twl4030_read(u8 address)
  96. {
  97. u8 data;
  98. int ret = 0;
  99. ret = twl_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
  100. return (ret < 0) ? ret : data;
  101. }
  102. /*----------------------------------------------------------------------*/
  103. static u8 cached_leden; /* protected by gpio_lock */
  104. /* The LED lines are open drain outputs ... a FET pulls to GND, so an
  105. * external pullup is needed. We could also expose the integrated PWM
  106. * as a LED brightness control; we initialize it as "always on".
  107. */
  108. static void twl4030_led_set_value(int led, int value)
  109. {
  110. u8 mask = LEDEN_LEDAON | LEDEN_LEDAPWM;
  111. int status;
  112. if (led)
  113. mask <<= 1;
  114. mutex_lock(&gpio_lock);
  115. if (value)
  116. cached_leden &= ~mask;
  117. else
  118. cached_leden |= mask;
  119. status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
  120. TWL4030_LED_LEDEN);
  121. mutex_unlock(&gpio_lock);
  122. }
  123. static int twl4030_set_gpio_direction(int gpio, int is_input)
  124. {
  125. u8 d_bnk = gpio >> 3;
  126. u8 d_msk = BIT(gpio & 0x7);
  127. u8 reg = 0;
  128. u8 base = REG_GPIODATADIR1 + d_bnk;
  129. int ret = 0;
  130. mutex_lock(&gpio_lock);
  131. ret = gpio_twl4030_read(base);
  132. if (ret >= 0) {
  133. if (is_input)
  134. reg = ret & ~d_msk;
  135. else
  136. reg = ret | d_msk;
  137. ret = gpio_twl4030_write(base, reg);
  138. }
  139. mutex_unlock(&gpio_lock);
  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. if (unlikely((gpio >= TWL4030_GPIO_MAX)
  160. || !(gpio_usage_count & BIT(gpio))))
  161. return -EPERM;
  162. base = REG_GPIODATAIN1 + d_bnk;
  163. ret = gpio_twl4030_read(base);
  164. if (ret > 0)
  165. ret = (ret >> d_off) & 0x1;
  166. return ret;
  167. }
  168. /*----------------------------------------------------------------------*/
  169. static int twl_request(struct gpio_chip *chip, unsigned offset)
  170. {
  171. int status = 0;
  172. mutex_lock(&gpio_lock);
  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 module = TWL4030_MODULE_PWMA;
  178. offset -= TWL4030_GPIO_MAX;
  179. if (offset) {
  180. ledclr_mask <<= 1;
  181. module = TWL4030_MODULE_PWMB;
  182. }
  183. /* initialize PWM to always-drive */
  184. status = twl_i2c_write_u8(module, 0x7f,
  185. TWL4030_PWMx_PWMxOFF);
  186. if (status < 0)
  187. goto done;
  188. status = twl_i2c_write_u8(module, 0x7f,
  189. TWL4030_PWMx_PWMxON);
  190. if (status < 0)
  191. goto done;
  192. /* init LED to not-driven (high) */
  193. module = TWL4030_MODULE_LED;
  194. status = twl_i2c_read_u8(module, &cached_leden,
  195. TWL4030_LED_LEDEN);
  196. if (status < 0)
  197. goto done;
  198. cached_leden &= ~ledclr_mask;
  199. status = twl_i2c_write_u8(module, cached_leden,
  200. TWL4030_LED_LEDEN);
  201. if (status < 0)
  202. goto done;
  203. status = 0;
  204. goto done;
  205. }
  206. /* on first use, turn GPIO module "on" */
  207. if (!gpio_usage_count) {
  208. struct twl4030_gpio_platform_data *pdata;
  209. u8 value = MASK_GPIO_CTRL_GPIO_ON;
  210. /* optionally have the first two GPIOs switch vMMC1
  211. * and vMMC2 power supplies based on card presence.
  212. */
  213. pdata = chip->dev->platform_data;
  214. if (pdata)
  215. value |= pdata->mmc_cd & 0x03;
  216. status = gpio_twl4030_write(REG_GPIO_CTRL, value);
  217. }
  218. if (!status)
  219. gpio_usage_count |= (0x1 << offset);
  220. done:
  221. mutex_unlock(&gpio_lock);
  222. return status;
  223. }
  224. static void twl_free(struct gpio_chip *chip, unsigned offset)
  225. {
  226. if (offset >= TWL4030_GPIO_MAX) {
  227. twl4030_led_set_value(offset - TWL4030_GPIO_MAX, 1);
  228. return;
  229. }
  230. mutex_lock(&gpio_lock);
  231. gpio_usage_count &= ~BIT(offset);
  232. /* on last use, switch off GPIO module */
  233. if (!gpio_usage_count)
  234. gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
  235. mutex_unlock(&gpio_lock);
  236. }
  237. static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
  238. {
  239. return (offset < TWL4030_GPIO_MAX)
  240. ? twl4030_set_gpio_direction(offset, 1)
  241. : -EINVAL;
  242. }
  243. static int twl_get(struct gpio_chip *chip, unsigned offset)
  244. {
  245. int status = 0;
  246. if (offset < TWL4030_GPIO_MAX)
  247. status = twl4030_get_gpio_datain(offset);
  248. else if (offset == TWL4030_GPIO_MAX)
  249. status = cached_leden & LEDEN_LEDAON;
  250. else
  251. status = cached_leden & LEDEN_LEDBON;
  252. return (status < 0) ? 0 : status;
  253. }
  254. static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
  255. {
  256. if (offset < TWL4030_GPIO_MAX) {
  257. twl4030_set_gpio_dataout(offset, value);
  258. return twl4030_set_gpio_direction(offset, 0);
  259. } else {
  260. twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
  261. return 0;
  262. }
  263. }
  264. static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
  265. {
  266. if (offset < TWL4030_GPIO_MAX)
  267. twl4030_set_gpio_dataout(offset, value);
  268. else
  269. twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
  270. }
  271. static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
  272. {
  273. return (twl4030_gpio_irq_base && (offset < TWL4030_GPIO_MAX))
  274. ? (twl4030_gpio_irq_base + offset)
  275. : -EINVAL;
  276. }
  277. static struct gpio_chip twl_gpiochip = {
  278. .label = "twl4030",
  279. .owner = THIS_MODULE,
  280. .request = twl_request,
  281. .free = twl_free,
  282. .direction_input = twl_direction_in,
  283. .get = twl_get,
  284. .direction_output = twl_direction_out,
  285. .set = twl_set,
  286. .to_irq = twl_to_irq,
  287. .can_sleep = 1,
  288. };
  289. /*----------------------------------------------------------------------*/
  290. static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
  291. {
  292. u8 message[6];
  293. unsigned i, gpio_bit;
  294. /* For most pins, a pulldown was enabled by default.
  295. * We should have data that's specific to this board.
  296. */
  297. for (gpio_bit = 1, i = 1; i < 6; i++) {
  298. u8 bit_mask;
  299. unsigned j;
  300. for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
  301. if (ups & gpio_bit)
  302. bit_mask |= 1 << (j + 1);
  303. else if (downs & gpio_bit)
  304. bit_mask |= 1 << (j + 0);
  305. }
  306. message[i] = bit_mask;
  307. }
  308. return twl_i2c_write(TWL4030_MODULE_GPIO, message,
  309. REG_GPIOPUPDCTR1, 5);
  310. }
  311. static int __devinit gpio_twl4030_debounce(u32 debounce, u8 mmc_cd)
  312. {
  313. u8 message[4];
  314. /* 30 msec of debouncing is always used for MMC card detect,
  315. * and is optional for everything else.
  316. */
  317. message[1] = (debounce & 0xff) | (mmc_cd & 0x03);
  318. debounce >>= 8;
  319. message[2] = (debounce & 0xff);
  320. debounce >>= 8;
  321. message[3] = (debounce & 0x03);
  322. return twl_i2c_write(TWL4030_MODULE_GPIO, message,
  323. REG_GPIO_DEBEN1, 3);
  324. }
  325. static int gpio_twl4030_remove(struct platform_device *pdev);
  326. static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
  327. {
  328. struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
  329. struct device_node *node = pdev->dev.of_node;
  330. int ret, irq_base;
  331. /* maybe setup IRQs */
  332. if (is_module()) {
  333. dev_err(&pdev->dev, "can't dispatch IRQs from modules\n");
  334. goto no_irqs;
  335. }
  336. irq_base = irq_alloc_descs(-1, 0, TWL4030_GPIO_MAX, 0);
  337. if (irq_base < 0) {
  338. dev_err(&pdev->dev, "Failed to alloc irq_descs\n");
  339. return irq_base;
  340. }
  341. irq_domain_add_legacy(node, TWL4030_GPIO_MAX, irq_base, 0,
  342. &irq_domain_simple_ops, NULL);
  343. ret = twl4030_sih_setup(&pdev->dev, TWL4030_MODULE_GPIO, irq_base);
  344. if (ret < 0)
  345. return ret;
  346. twl4030_gpio_irq_base = irq_base;
  347. no_irqs:
  348. twl_gpiochip.base = -1;
  349. twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
  350. twl_gpiochip.dev = &pdev->dev;
  351. if (pdata) {
  352. twl_gpiochip.base = pdata->gpio_base;
  353. /*
  354. * NOTE: boards may waste power if they don't set pullups
  355. * and pulldowns correctly ... default for non-ULPI pins is
  356. * pulldown, and some other pins may have external pullups
  357. * or pulldowns. Careful!
  358. */
  359. ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
  360. if (ret)
  361. dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
  362. pdata->pullups, pdata->pulldowns,
  363. ret);
  364. ret = gpio_twl4030_debounce(pdata->debounce, pdata->mmc_cd);
  365. if (ret)
  366. dev_dbg(&pdev->dev, "debounce %.03x %.01x --> %d\n",
  367. pdata->debounce, pdata->mmc_cd,
  368. ret);
  369. /*
  370. * NOTE: we assume VIBRA_CTL.VIBRA_EN, in MODULE_AUDIO_VOICE,
  371. * is (still) clear if use_leds is set.
  372. */
  373. if (pdata->use_leds)
  374. twl_gpiochip.ngpio += 2;
  375. }
  376. ret = gpiochip_add(&twl_gpiochip);
  377. if (ret < 0) {
  378. dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
  379. twl_gpiochip.ngpio = 0;
  380. gpio_twl4030_remove(pdev);
  381. } else if (pdata && pdata->setup) {
  382. int status;
  383. status = pdata->setup(&pdev->dev,
  384. pdata->gpio_base, TWL4030_GPIO_MAX);
  385. if (status)
  386. dev_dbg(&pdev->dev, "setup --> %d\n", status);
  387. }
  388. return ret;
  389. }
  390. /* Cannot use __devexit as gpio_twl4030_probe() calls us */
  391. static int gpio_twl4030_remove(struct platform_device *pdev)
  392. {
  393. struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
  394. int status;
  395. if (pdata && pdata->teardown) {
  396. status = pdata->teardown(&pdev->dev,
  397. pdata->gpio_base, TWL4030_GPIO_MAX);
  398. if (status) {
  399. dev_dbg(&pdev->dev, "teardown --> %d\n", status);
  400. return status;
  401. }
  402. }
  403. status = gpiochip_remove(&twl_gpiochip);
  404. if (status < 0)
  405. return status;
  406. if (is_module())
  407. return 0;
  408. /* REVISIT no support yet for deregistering all the IRQs */
  409. WARN_ON(1);
  410. return -EIO;
  411. }
  412. static const struct of_device_id twl_gpio_match[] = {
  413. { .compatible = "ti,twl4030-gpio", },
  414. { },
  415. };
  416. MODULE_DEVICE_TABLE(of, twl_gpio_match);
  417. /* Note: this hardware lives inside an I2C-based multi-function device. */
  418. MODULE_ALIAS("platform:twl4030_gpio");
  419. static struct platform_driver gpio_twl4030_driver = {
  420. .driver = {
  421. .name = "twl4030_gpio",
  422. .owner = THIS_MODULE,
  423. .of_match_table = of_match_ptr(twl_gpio_match),
  424. },
  425. .probe = gpio_twl4030_probe,
  426. .remove = gpio_twl4030_remove,
  427. };
  428. static int __init gpio_twl4030_init(void)
  429. {
  430. return platform_driver_register(&gpio_twl4030_driver);
  431. }
  432. subsys_initcall(gpio_twl4030_init);
  433. static void __exit gpio_twl4030_exit(void)
  434. {
  435. platform_driver_unregister(&gpio_twl4030_driver);
  436. }
  437. module_exit(gpio_twl4030_exit);
  438. MODULE_AUTHOR("Texas Instruments, Inc.");
  439. MODULE_DESCRIPTION("GPIO interface for TWL4030");
  440. MODULE_LICENSE("GPL");