twl4030-gpio.c 12 KB

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