twl4030-gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. * Configure debounce timing value for a GPIO pin on TWL4030
  169. */
  170. int twl4030_set_gpio_debounce(int gpio, int enable)
  171. {
  172. u8 d_bnk = gpio >> 3;
  173. u8 d_msk = BIT(gpio & 0x7);
  174. u8 reg = 0;
  175. u8 base = 0;
  176. int ret = 0;
  177. if (unlikely((gpio >= TWL4030_GPIO_MAX)
  178. || !(gpio_usage_count & BIT(gpio))))
  179. return -EPERM;
  180. base = REG_GPIO_DEBEN1 + d_bnk;
  181. mutex_lock(&gpio_lock);
  182. ret = gpio_twl4030_read(base);
  183. if (ret >= 0) {
  184. if (enable)
  185. reg = ret | d_msk;
  186. else
  187. reg = ret & ~d_msk;
  188. ret = gpio_twl4030_write(base, reg);
  189. }
  190. mutex_unlock(&gpio_lock);
  191. return ret;
  192. }
  193. EXPORT_SYMBOL(twl4030_set_gpio_debounce);
  194. /*----------------------------------------------------------------------*/
  195. static int twl_request(struct gpio_chip *chip, unsigned offset)
  196. {
  197. int status = 0;
  198. mutex_lock(&gpio_lock);
  199. /* Support the two LED outputs as output-only GPIOs. */
  200. if (offset >= TWL4030_GPIO_MAX) {
  201. u8 ledclr_mask = LEDEN_LEDAON | LEDEN_LEDAEXT
  202. | LEDEN_LEDAPWM | LEDEN_PWM_LENGTHA;
  203. u8 module = TWL4030_MODULE_PWMA;
  204. offset -= TWL4030_GPIO_MAX;
  205. if (offset) {
  206. ledclr_mask <<= 1;
  207. module = TWL4030_MODULE_PWMB;
  208. }
  209. /* initialize PWM to always-drive */
  210. status = twl4030_i2c_write_u8(module, 0x7f,
  211. TWL4030_PWMx_PWMxOFF);
  212. if (status < 0)
  213. goto done;
  214. status = twl4030_i2c_write_u8(module, 0x7f,
  215. TWL4030_PWMx_PWMxON);
  216. if (status < 0)
  217. goto done;
  218. /* init LED to not-driven (high) */
  219. module = TWL4030_MODULE_LED;
  220. status = twl4030_i2c_read_u8(module, &cached_leden,
  221. TWL4030_LED_LEDEN);
  222. if (status < 0)
  223. goto done;
  224. cached_leden &= ~ledclr_mask;
  225. status = twl4030_i2c_write_u8(module, cached_leden,
  226. TWL4030_LED_LEDEN);
  227. if (status < 0)
  228. goto done;
  229. status = 0;
  230. goto done;
  231. }
  232. /* on first use, turn GPIO module "on" */
  233. if (!gpio_usage_count) {
  234. struct twl4030_gpio_platform_data *pdata;
  235. u8 value = MASK_GPIO_CTRL_GPIO_ON;
  236. /* optionally have the first two GPIOs switch vMMC1
  237. * and vMMC2 power supplies based on card presence.
  238. */
  239. pdata = chip->dev->platform_data;
  240. value |= pdata->mmc_cd & 0x03;
  241. status = gpio_twl4030_write(REG_GPIO_CTRL, value);
  242. }
  243. if (!status)
  244. gpio_usage_count |= (0x1 << offset);
  245. done:
  246. mutex_unlock(&gpio_lock);
  247. return status;
  248. }
  249. static void twl_free(struct gpio_chip *chip, unsigned offset)
  250. {
  251. if (offset >= TWL4030_GPIO_MAX) {
  252. twl4030_led_set_value(offset - TWL4030_GPIO_MAX, 1);
  253. return;
  254. }
  255. mutex_lock(&gpio_lock);
  256. gpio_usage_count &= ~BIT(offset);
  257. /* on last use, switch off GPIO module */
  258. if (!gpio_usage_count)
  259. gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
  260. mutex_unlock(&gpio_lock);
  261. }
  262. static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
  263. {
  264. return (offset < TWL4030_GPIO_MAX)
  265. ? twl4030_set_gpio_direction(offset, 1)
  266. : -EINVAL;
  267. }
  268. static int twl_get(struct gpio_chip *chip, unsigned offset)
  269. {
  270. int status = 0;
  271. if (offset < TWL4030_GPIO_MAX)
  272. status = twl4030_get_gpio_datain(offset);
  273. else if (offset == TWL4030_GPIO_MAX)
  274. status = cached_leden & LEDEN_LEDAON;
  275. else
  276. status = cached_leden & LEDEN_LEDBON;
  277. return (status < 0) ? 0 : status;
  278. }
  279. static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
  280. {
  281. if (offset < TWL4030_GPIO_MAX) {
  282. twl4030_set_gpio_dataout(offset, value);
  283. return twl4030_set_gpio_direction(offset, 0);
  284. } else {
  285. twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
  286. return 0;
  287. }
  288. }
  289. static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
  290. {
  291. if (offset < TWL4030_GPIO_MAX)
  292. twl4030_set_gpio_dataout(offset, value);
  293. else
  294. twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
  295. }
  296. static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
  297. {
  298. return (twl4030_gpio_irq_base && (offset < TWL4030_GPIO_MAX))
  299. ? (twl4030_gpio_irq_base + offset)
  300. : -EINVAL;
  301. }
  302. static struct gpio_chip twl_gpiochip = {
  303. .label = "twl4030",
  304. .owner = THIS_MODULE,
  305. .request = twl_request,
  306. .free = twl_free,
  307. .direction_input = twl_direction_in,
  308. .get = twl_get,
  309. .direction_output = twl_direction_out,
  310. .set = twl_set,
  311. .to_irq = twl_to_irq,
  312. .can_sleep = 1,
  313. };
  314. /*----------------------------------------------------------------------*/
  315. static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
  316. {
  317. u8 message[6];
  318. unsigned i, gpio_bit;
  319. /* For most pins, a pulldown was enabled by default.
  320. * We should have data that's specific to this board.
  321. */
  322. for (gpio_bit = 1, i = 1; i < 6; i++) {
  323. u8 bit_mask;
  324. unsigned j;
  325. for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
  326. if (ups & gpio_bit)
  327. bit_mask |= 1 << (j + 1);
  328. else if (downs & gpio_bit)
  329. bit_mask |= 1 << (j + 0);
  330. }
  331. message[i] = bit_mask;
  332. }
  333. return twl4030_i2c_write(TWL4030_MODULE_GPIO, message,
  334. REG_GPIOPUPDCTR1, 5);
  335. }
  336. static int gpio_twl4030_remove(struct platform_device *pdev);
  337. static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
  338. {
  339. struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
  340. int ret;
  341. /* maybe setup IRQs */
  342. if (pdata->irq_base) {
  343. if (is_module()) {
  344. dev_err(&pdev->dev,
  345. "can't dispatch IRQs from modules\n");
  346. goto no_irqs;
  347. }
  348. ret = twl4030_sih_setup(TWL4030_MODULE_GPIO);
  349. if (ret < 0)
  350. return ret;
  351. WARN_ON(ret != pdata->irq_base);
  352. twl4030_gpio_irq_base = ret;
  353. }
  354. no_irqs:
  355. /*
  356. * NOTE: boards may waste power if they don't set pullups
  357. * and pulldowns correctly ... default for non-ULPI pins is
  358. * pulldown, and some other pins may have external pullups
  359. * or pulldowns. Careful!
  360. */
  361. ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
  362. if (ret)
  363. dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
  364. pdata->pullups, pdata->pulldowns,
  365. ret);
  366. twl_gpiochip.base = pdata->gpio_base;
  367. twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
  368. twl_gpiochip.dev = &pdev->dev;
  369. /* NOTE: we assume VIBRA_CTL.VIBRA_EN, in MODULE_AUDIO_VOICE,
  370. * is (still) clear if use_leds is set.
  371. */
  372. if (pdata->use_leds)
  373. twl_gpiochip.ngpio += 2;
  374. ret = gpiochip_add(&twl_gpiochip);
  375. if (ret < 0) {
  376. dev_err(&pdev->dev,
  377. "could not register gpiochip, %d\n",
  378. ret);
  379. twl_gpiochip.ngpio = 0;
  380. gpio_twl4030_remove(pdev);
  381. } else if (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. static int __devexit gpio_twl4030_remove(struct platform_device *pdev)
  391. {
  392. struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
  393. int status;
  394. if (pdata->teardown) {
  395. status = pdata->teardown(&pdev->dev,
  396. pdata->gpio_base, TWL4030_GPIO_MAX);
  397. if (status) {
  398. dev_dbg(&pdev->dev, "teardown --> %d\n", status);
  399. return status;
  400. }
  401. }
  402. status = gpiochip_remove(&twl_gpiochip);
  403. if (status < 0)
  404. return status;
  405. if (is_module())
  406. return 0;
  407. /* REVISIT no support yet for deregistering all the IRQs */
  408. WARN_ON(1);
  409. return -EIO;
  410. }
  411. /* Note: this hardware lives inside an I2C-based multi-function device. */
  412. MODULE_ALIAS("platform:twl4030_gpio");
  413. static struct platform_driver gpio_twl4030_driver = {
  414. .driver.name = "twl4030_gpio",
  415. .driver.owner = THIS_MODULE,
  416. .probe = gpio_twl4030_probe,
  417. .remove = __devexit_p(gpio_twl4030_remove),
  418. };
  419. static int __init gpio_twl4030_init(void)
  420. {
  421. return platform_driver_register(&gpio_twl4030_driver);
  422. }
  423. subsys_initcall(gpio_twl4030_init);
  424. static void __exit gpio_twl4030_exit(void)
  425. {
  426. platform_driver_unregister(&gpio_twl4030_driver);
  427. }
  428. module_exit(gpio_twl4030_exit);
  429. MODULE_AUTHOR("Texas Instruments, Inc.");
  430. MODULE_DESCRIPTION("GPIO interface for TWL4030");
  431. MODULE_LICENSE("GPL");