twl4030-gpio.c 12 KB

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