gpio-twl4030.c 14 KB

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