gpio-twl4030.c 14 KB

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