gpio-pcal6416a.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. * pcal6416a.c - 16 bit I/O port
  3. *
  4. * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
  5. * Copyright (C) 2007 Marvell International Ltd.
  6. * Copyright (C) 2013 NXP Semiconductors
  7. *
  8. * Derived from drivers/i2c/chips/pca953x.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/i2c.h>
  19. #include <linux/gpio.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/delay.h>
  24. #include <linux/err.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/seq_file.h>
  28. #if defined(CONFIG_OF)
  29. #include <linux/of_device.h>
  30. #include <linux/of_gpio.h>
  31. #endif
  32. #include <linux/i2c/pcal6416a.h>
  33. #define DRV_NAME "pcal6416a-gpio"
  34. #define POWER_ON 1
  35. #define POWER_OFF 0
  36. struct pcal6416a_chip {
  37. struct i2c_client *client;
  38. struct gpio_chip gpio_chip;
  39. struct dentry *dentry;
  40. struct mutex lock;
  41. struct pcal6416a_platform_data *pdata;
  42. unsigned gpio_start;
  43. uint16_t reg_output;
  44. uint16_t reg_polarity;
  45. uint16_t reg_config;
  46. uint16_t reg_drive0;
  47. uint16_t reg_drive1;
  48. uint16_t reg_inputlatch;
  49. uint16_t reg_enpullupdown;
  50. uint16_t reg_selpullupdown;
  51. uint16_t reg_intmask;
  52. uint16_t reg_outputconfig;
  53. };
  54. struct pcal6416a_chip *g_dev = NULL;
  55. /* read the 16-bit register from the PCAL6416A
  56. reg: register address
  57. val: the value read back from the PCAL6416A
  58. */
  59. static int pcal6416a_read_reg(struct pcal6416a_chip *chip, int reg, uint16_t *val)
  60. {
  61. int ret = i2c_smbus_read_word_data(chip->client, reg);
  62. if (ret < 0) {
  63. dev_err(&chip->client->dev,
  64. "failed reading register %d\n", ret);
  65. return ret;
  66. }
  67. *val = (uint16_t)ret;
  68. return 0;
  69. }
  70. /* write a 16-bit value to the PCAL6416A
  71. reg: register address
  72. val: the value read back from the PCAL6416A
  73. */
  74. static int pcal6416a_write_reg(struct pcal6416a_chip *chip, int reg, uint16_t val)
  75. {
  76. int ret = i2c_smbus_write_word_data(chip->client, reg, val);
  77. if (ret < 0) {
  78. dev_err(&chip->client->dev,
  79. "failed writing register %d\n", ret);
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. /* read a port pin value (INPUT register) from the PCAL6416A
  85. off: bit number (0..15)
  86. return: bit value 0 or 1
  87. */
  88. static int pcal6416a_gpio_get_value(struct gpio_chip *gc, unsigned off)
  89. {
  90. uint16_t reg_val;
  91. int ret;
  92. struct pcal6416a_chip *chip
  93. = container_of(gc, struct pcal6416a_chip, gpio_chip);
  94. ret = pcal6416a_read_reg(chip, PCAL6416A_INPUT, &reg_val);
  95. if (ret < 0) {
  96. /* NOTE: diagnostic already emitted; that's all we should
  97. * do unless gpio_*_value_cansleep() calls become different
  98. * from their nonsleeping siblings (and report faults).
  99. */
  100. return ret;
  101. }
  102. return (reg_val & (1u << off)) ? 1 : 0;
  103. }
  104. /* write a port pin value (INPUT register) from the PCAL6416A
  105. off: bit number (0..15)
  106. val: 0 or 1
  107. return: none
  108. */
  109. static void pcal6416a_gpio_set_value(struct gpio_chip *gc,
  110. unsigned off, int val)
  111. {
  112. struct pcal6416a_chip *chip
  113. = container_of(gc, struct pcal6416a_chip, gpio_chip);
  114. mutex_lock(&chip->lock);
  115. if (val)
  116. chip->reg_output |= (1u << off);
  117. else
  118. chip->reg_output &= ~(1u << off);
  119. pcal6416a_write_reg(chip, PCAL6416A_DAT_OUT, chip->reg_output);
  120. mutex_unlock(&chip->lock);
  121. }
  122. /* set the CONFIGURATION register of a port pin as an input
  123. off: bit number (0..15)
  124. */
  125. static int pcal6416a_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  126. {
  127. int ret;
  128. struct pcal6416a_chip *chip
  129. = container_of(gc, struct pcal6416a_chip, gpio_chip);
  130. pr_info("%s(off=[%d])\n", __func__, off);
  131. mutex_lock(&chip->lock);
  132. /* input set bit 1 */
  133. chip->reg_config |= (1u << off);
  134. ret = pcal6416a_write_reg(chip, PCAL6416A_CONFIG, chip->reg_config);
  135. mutex_unlock(&chip->lock);
  136. return ret;
  137. }
  138. /* set the DIRECTION (CONFIGURATION register) of a port pin as an output
  139. off: bit number (0..15)
  140. val = 1 or 0
  141. return: 0 if successful
  142. */
  143. static int pcal6416a_gpio_direction_output(struct gpio_chip *gc,
  144. unsigned off, int val)
  145. {
  146. int ret;
  147. struct pcal6416a_chip *chip
  148. = container_of(gc, struct pcal6416a_chip, gpio_chip);
  149. pr_info("%s(off=[%d], val=[%d])\n", __func__, off, val);
  150. mutex_lock(&chip->lock);
  151. /* set output level */
  152. if (val)
  153. chip->reg_output |= (1u << off);
  154. else
  155. chip->reg_output &= ~(1u << off);
  156. ret = pcal6416a_write_reg(chip, PCAL6416A_DAT_OUT, chip->reg_output);
  157. /* then direction */
  158. /* output set bit 0 */
  159. chip->reg_config &= ~(1u << off);
  160. ret |= pcal6416a_write_reg(chip, PCAL6416A_CONFIG, chip->reg_config);
  161. mutex_unlock(&chip->lock);
  162. return ret;
  163. }
  164. static int pcal6416a_gpio_request(struct gpio_chip *gc, unsigned off)
  165. {
  166. struct pcal6416a_chip *chip
  167. = container_of(gc, struct pcal6416a_chip, gpio_chip);
  168. pr_info("%s\n", __func__);
  169. if (off >= chip->gpio_chip.ngpio) {
  170. pr_err("[%s] offset over max = [%d]\n", __func__, off);
  171. return 1;
  172. }
  173. /* to do*/
  174. return 0;
  175. }
  176. static void pcal6416a_gpio_free(struct gpio_chip *gc, unsigned off)
  177. {
  178. struct pcal6416a_chip *chip
  179. = container_of(gc, struct pcal6416a_chip, gpio_chip);
  180. pr_info("%s\n", __func__);
  181. if (off >= chip->gpio_chip.ngpio) {
  182. pr_err("[%s] offset over max = [%d]\n", __func__, off);
  183. }
  184. /* to do*/
  185. }
  186. int expander_gpio_config(unsigned config, unsigned disable)
  187. {
  188. int ret;
  189. unsigned min_gpio, max_gpio, offset;
  190. unsigned gpio = GPIO_PIN(config);
  191. unsigned dir = GPIO_DIR(config);
  192. unsigned pull = GPIO_PULL(config);
  193. unsigned drvstr = GPIO_DRVSTR(config);
  194. min_gpio = g_dev->gpio_start;
  195. max_gpio = g_dev->gpio_start + g_dev->gpio_chip.ngpio;
  196. pr_info("[%s]config gpio [%d]\n", __func__, gpio);
  197. if (!((min_gpio <= gpio) && (gpio < max_gpio)))
  198. return -EINVAL;
  199. offset = gpio-min_gpio;
  200. if (dir) { /* output */
  201. pcal6416a_gpio_direction_output(&g_dev->gpio_chip, offset, 0);
  202. } else {
  203. pcal6416a_gpio_direction_input(&g_dev->gpio_chip, offset);
  204. }
  205. switch (pull) { /* pull-up, pull-down, no-pull */
  206. case GPIO_CFG_NO_PULL:
  207. g_dev->reg_enpullupdown &= ~(1<<offset);
  208. g_dev->reg_selpullupdown &= ~(1<<offset);
  209. break;
  210. case GPIO_CFG_PULL_DOWN:
  211. g_dev->reg_enpullupdown |= (1<<offset);
  212. g_dev->reg_selpullupdown &= ~(1<<offset);
  213. break;
  214. case GPIO_CFG_PULL_UP:
  215. g_dev->reg_enpullupdown |= (1<<offset);
  216. g_dev->reg_selpullupdown |= (1<<offset);
  217. break;
  218. default:
  219. pr_err("Not support config - GPIO_CFG_KEEPER\n");
  220. break;
  221. }
  222. ret = pcal6416a_write_reg(g_dev, PCAL6416A_EN_PULLUPDOWN,
  223. g_dev->reg_enpullupdown);
  224. if (ret < 0)
  225. pr_err("failed set enable pullupdown\n");
  226. ret = pcal6416a_write_reg(g_dev, PCAL6416A_SEL_PULLUPDOWN,
  227. g_dev->reg_selpullupdown);
  228. if (ret < 0)
  229. pr_err("failed set select pull\n");
  230. if (offset < 8) { /* p0 ~ p7 */
  231. switch (drvstr) {
  232. case GPIO_CFG_6_25MA:
  233. g_dev->reg_drive0 &= ~(0x3<<(offset*2));
  234. break;
  235. case GPIO_CFG_12_5MA:
  236. g_dev->reg_drive0 &= ~(0x3<<(offset*2));
  237. g_dev->reg_drive0 |= (0x1<<(offset*2));
  238. break;
  239. case GPIO_CFG_18_75MA:
  240. g_dev->reg_drive0 &= ~(0x3<<(offset*2));
  241. g_dev->reg_drive0 |= (0x2<<(offset*2));
  242. break;
  243. case GPIO_CFG_25MA:
  244. g_dev->reg_drive0 |= (0x3<<(offset*2));
  245. break;
  246. default:
  247. pr_err("Not support config - drive strength\n");
  248. break;
  249. }
  250. ret = pcal6416a_write_reg(g_dev, PCAL6416A_DRIVE0,
  251. g_dev->reg_drive0);
  252. if (ret < 0)
  253. pr_err("failed set drive0\n");
  254. } else {
  255. switch (drvstr) {
  256. case GPIO_CFG_6_25MA:
  257. g_dev->reg_drive1 &= ~(0x3<<((offset-8)*2));
  258. break;
  259. case GPIO_CFG_12_5MA:
  260. g_dev->reg_drive1 &= ~(0x3<<((offset-8)*2));
  261. g_dev->reg_drive1 |= (0x1<<((offset-8)*2));
  262. break;
  263. case GPIO_CFG_18_75MA:
  264. g_dev->reg_drive1 &= ~(0x3<<((offset-8)*2));
  265. g_dev->reg_drive1 |= (0x2<<((offset-8)*2));
  266. break;
  267. case GPIO_CFG_25MA:
  268. g_dev->reg_drive1 |= (0x3<<((offset-8)*2));
  269. break;
  270. default:
  271. pr_err("Not support config - drive strength\n");
  272. break;
  273. }
  274. ret = pcal6416a_write_reg(g_dev, PCAL6416A_DRIVE1,
  275. g_dev->reg_drive1);
  276. if (ret < 0)
  277. pr_err("failed set drive1\n");
  278. }
  279. return 0;
  280. }
  281. EXPORT_SYMBOL(expander_gpio_config);
  282. static int __devinit pcal6416a_gpio_setup(struct pcal6416a_chip *dev)
  283. {
  284. int ret;
  285. uint16_t read_val;
  286. pr_info("[%s] GPIO Expander Init setting\n", __func__);
  287. dev->reg_outputconfig = 0x0000;
  288. ret = pcal6416a_write_reg(dev, PCAL6416A_OUTPUT_CONFIG,
  289. dev->reg_outputconfig); /* push-pull */
  290. if (ret < 0) {
  291. pr_err("failed set output config\n");
  292. return ret;
  293. }
  294. ret = pcal6416a_write_reg(dev, PCAL6416A_DAT_OUT,
  295. dev->reg_output); /* 1 : output high, 0 : output low */
  296. if (ret < 0) {
  297. pr_err("failed set data out\n");
  298. return ret;
  299. }
  300. ret = pcal6416a_write_reg(dev, PCAL6416A_CONFIG,
  301. dev->reg_config); /* 1 : input, 0 : output */
  302. if (ret < 0) {
  303. pr_err("failed set config\n");
  304. return ret;
  305. }
  306. dev->reg_polarity = 0x0000;
  307. ret = pcal6416a_write_reg(dev, PCAL6416A_POLARITY,
  308. dev->reg_polarity);
  309. if (ret < 0) {
  310. pr_err("failed set polarity\n");
  311. return ret;
  312. }
  313. dev->reg_drive0 = 0x0000;
  314. ret = pcal6416a_write_reg(dev, PCAL6416A_DRIVE0,
  315. dev->reg_drive0); /* drive 0.25x */
  316. if (ret < 0) {
  317. pr_err("failed set drive0\n");
  318. return ret;
  319. }
  320. dev->reg_drive1 = 0x0000;
  321. ret = pcal6416a_write_reg(dev, PCAL6416A_DRIVE1,
  322. dev->reg_drive1); /* drive 0.25x */
  323. if (ret < 0) {
  324. pr_err("failed set drive1\n");
  325. return ret;
  326. }
  327. dev->reg_inputlatch = 0x0000;
  328. ret = pcal6416a_write_reg(dev, PCAL6416A_INPUT_LATCH,
  329. dev->reg_inputlatch); /* not use latch */
  330. if (ret < 0) {
  331. pr_err("failed set input latch\n");
  332. return ret;
  333. }
  334. ret = pcal6416a_write_reg(dev, PCAL6416A_EN_PULLUPDOWN,
  335. dev->reg_enpullupdown); /* 1 : enable, 0 : disable */
  336. if (ret < 0) {
  337. pr_err("failed set enable pullupdown\n");
  338. return ret;
  339. }
  340. ret = pcal6416a_write_reg(dev, PCAL6416A_SEL_PULLUPDOWN,
  341. dev->reg_selpullupdown); /* 1 : pull-up, 0 : pull-down */
  342. if (ret < 0) {
  343. pr_err("failed set select pull\n");
  344. return ret;
  345. }
  346. dev->reg_intmask = 0xFFFF;
  347. ret = pcal6416a_write_reg(dev, PCAL6416A_INT_MASK,
  348. dev->reg_intmask); /* not use int */
  349. if (ret < 0) {
  350. pr_err("failed set int mask\n");
  351. return ret;
  352. }
  353. ret = pcal6416a_read_reg(dev, PCAL6416A_INT_MASK,
  354. &read_val);
  355. if (ret < 0) {
  356. pr_err("failed read int mask\n");
  357. return ret;
  358. }
  359. return 0;
  360. }
  361. #ifdef CONFIG_OF
  362. static int pcal6416a_parse_dt(struct device *dev,
  363. struct pcal6416a_platform_data *pdata)
  364. {
  365. struct device_node *np = dev->of_node;
  366. int ret, i;
  367. u32 pull_reg;
  368. ret = of_property_read_u32(np, "pcal6416a,gpio_start", &pdata->gpio_start);
  369. if (ret < 0) {
  370. pr_err("[%s]: Unable to read pcal6416a,gpio_start\n", __func__);
  371. return ret;
  372. }
  373. ret = of_property_read_u32(np, "pcal6416a,ngpio", &pdata->ngpio);
  374. if (ret < 0) {
  375. pr_err("[%s]: Unable to read pcal6416a,ngpio\n", __func__);
  376. return ret;
  377. }
  378. pdata->reset_gpio = of_get_named_gpio(np, "pcal6416a,reset-gpio", 0);
  379. ret = of_property_read_u32(np, "pcal6416a,support_initialize", (u32 *)&pdata->support_init);
  380. if (ret < 0) {
  381. pr_err("[%s]: Unable to read pcal6416a,support_init\n", __func__);
  382. pdata->support_init = 0;
  383. }
  384. ret = of_property_read_string(np, "pcal6416a,supply-name", &pdata->supply_name);
  385. if (ret < 0) {
  386. pr_err("[%s]: Unable to read pcal6416a,supply-name\n", __func__);
  387. pdata->supply_name = "8084_s4";
  388. }
  389. if (pdata->support_init) {
  390. ret = of_property_read_u32(np, "pcal6416a,config", (u32 *)&pdata->init_config);
  391. if (ret < 0) {
  392. pr_err("[%s]: Unable to read pcal6416a,support_init\n", __func__);
  393. pdata->init_config= 0x0000;
  394. }
  395. ret = of_property_read_u32(np, "pcal6416a,data_out", (u32 *)&pdata->init_data_out);
  396. if (ret < 0) {
  397. pr_err("[%s]: Unable to read pcal6416a,support_init\n", __func__);
  398. pdata->init_data_out = 0x0000;
  399. }
  400. ret = of_property_read_u32(np, "pcal6416a,pull_reg", &pull_reg);
  401. if (ret < 0) {
  402. pr_err("[%s]: Unable to read pcal6416a,pull_reg\n", __func__);
  403. pdata->init_en_pull = 0x0000;
  404. pdata->init_sel_pull = 0x0000;
  405. }
  406. pr_info("[%s] 0x%08x\n", __func__, pull_reg);
  407. pdata->init_en_pull = 0x0000;
  408. pdata->init_sel_pull = 0x0000;
  409. for (i = 0; i < 16; i++) {
  410. if (((pull_reg>>(i*2))&0x3) == NO_PULL) {
  411. pdata->init_en_pull &= ~(1<<i);
  412. pdata->init_sel_pull &= ~(1<<i);
  413. }
  414. else if (((pull_reg>>(i*2))&0x3) == PULL_DOWN) {
  415. pdata->init_en_pull |= (1<<i);
  416. pdata->init_sel_pull &= ~(1<<i);
  417. }
  418. else if (((pull_reg>>(i*2))&0x3) == PULL_UP) {
  419. pdata->init_en_pull |= (1<<i);
  420. pdata->init_sel_pull |= (1<<i);
  421. }
  422. }
  423. } else {
  424. pdata->init_config = 0x0000;
  425. pdata->init_data_out = 0x0400;
  426. pdata->init_en_pull = 0x0000;
  427. pdata->init_sel_pull = 0x0000;
  428. }
  429. pr_info("[%s] initialize reg 0x%04x 0x%04x 0x%04x 0x%04x\n", __func__,
  430. pdata->init_config, pdata->init_data_out,
  431. pdata->init_en_pull, pdata->init_sel_pull);
  432. dev->platform_data = pdata;
  433. pr_info("[%s]gpio_start=[%d]ngpio=[%d]reset-gpio=[%d]\n",
  434. __func__, pdata->gpio_start, pdata->ngpio,
  435. pdata->reset_gpio);
  436. pr_info("[%s] supply-name=[%s]\n",
  437. __func__, pdata->supply_name);
  438. return 0;
  439. }
  440. #endif
  441. static void pcal6416a_power_ctrl(struct pcal6416a_platform_data *pdata, char enable)
  442. {
  443. int ret = 0;
  444. static struct regulator *reg_power;
  445. if (!reg_power) {
  446. reg_power = regulator_get(NULL, pdata->supply_name);
  447. if (IS_ERR(reg_power)) {
  448. pr_err("%s: could not get %s, rc = %ld\n",
  449. __func__, pdata->supply_name, PTR_ERR(reg_power));
  450. return;
  451. }
  452. ret = regulator_set_voltage(reg_power, 1800000, 1800000);
  453. if (ret) {
  454. pr_err("%s: unable to set power regulator voltage to 1.8V\n",
  455. __func__);
  456. return;
  457. }
  458. }
  459. if (enable) {
  460. if (regulator_is_enabled(reg_power))
  461. pr_err("%s: power regulator(1.8V) is enabled\n", __func__);
  462. else
  463. ret = regulator_enable(reg_power);
  464. if (ret) {
  465. pr_err("%s: power regulator enable failed, rc=%d\n",
  466. __func__, ret);
  467. return;
  468. }
  469. pr_info("%s: gpio expander 1.8V on is finished.\n", __func__);
  470. } else {
  471. if (regulator_is_enabled(reg_power))
  472. ret = regulator_disable(reg_power);
  473. else
  474. pr_err("%s: power regulator(1.8V) is disabled\n", __func__);
  475. if (ret) {
  476. pr_err("%s: disable power regulator failed, rc=%d\n",
  477. __func__, ret);
  478. return;
  479. }
  480. pr_info("%s: gpio expander 1.8V off is finished.\n", __func__);
  481. }
  482. pr_err("[pcal6416a gpio expander] %s enable(%d)\n", __func__, enable);
  483. return;
  484. }
  485. static int pcal6416a_reset_chip(struct pcal6416a_platform_data *pdata)
  486. {
  487. int retval;
  488. int reset_gpio = pdata->reset_gpio;
  489. if (gpio_is_valid(reset_gpio)) {
  490. retval = gpio_request(reset_gpio,
  491. "pcal6416a_reset_gpio");
  492. if (retval) {
  493. pr_err("[%s]: unable to request gpio [%d]\n",
  494. __func__, reset_gpio);
  495. return retval;
  496. }
  497. retval = gpio_direction_output(reset_gpio, 1);
  498. if (retval) {
  499. pr_err("[%s]: unable to set direction for gpio [%d]\n",
  500. __func__, reset_gpio);
  501. gpio_free(reset_gpio);
  502. return retval;
  503. }
  504. usleep(100);
  505. gpio_set_value(reset_gpio, 0);
  506. usleep(100);
  507. gpio_set_value(reset_gpio, 1);
  508. pr_info("[%s]: gpio expander reset.\n", __func__);
  509. gpio_free(reset_gpio);
  510. return 0;
  511. } else {
  512. pr_err("[%s]: gpio_is_valid fail\n", __func__);
  513. return -EIO;
  514. }
  515. return 0;
  516. }
  517. struct device *pcal6416a_dev;
  518. extern struct class *sec_class;
  519. static ssize_t store_pcal6416a_gpio_inout(struct device *dev,
  520. struct device_attribute *devattr,
  521. const char *buf, size_t count)
  522. {
  523. int retval, off, val, gpio_pcal6416a;
  524. char in_out, msg[13];
  525. struct pcal6416a_chip *data = dev_get_drvdata(dev);
  526. retval = sscanf(buf, "%1c %3d %1d", &in_out, &off, &val);
  527. if (retval == 0) {
  528. dev_err(&data->client->dev, "[%s] fail to pcal6416a out.\n", __func__);
  529. return count;
  530. }
  531. if (!(in_out == 'i' || in_out == 'o')) {
  532. pr_err("[%s] wrong in_out value [%c]\n", __func__, in_out);
  533. return count;
  534. }
  535. if ((off < 0) || (off > 15)) {
  536. pr_err("[%s] wrong offset value [%d]\n", __func__, off);
  537. return count;
  538. }
  539. if (!(val == 0 || val == 1)) {
  540. pr_err("[%s] wrong val value [%d]\n", __func__, val);
  541. return count;
  542. }
  543. gpio_pcal6416a = data->gpio_start + off;
  544. snprintf(msg, sizeof(msg)/sizeof(char), "exp-gpio%d\n", off);
  545. if (gpio_is_valid(gpio_pcal6416a)) {
  546. retval = gpio_request(gpio_pcal6416a, msg);
  547. if (retval) {
  548. pr_err("[%s] unable to request gpio=[%d] err=[%d]\n",
  549. __func__, gpio_pcal6416a, retval);
  550. return count;
  551. }
  552. if (in_out == 'i') {
  553. retval = gpio_direction_input(gpio_pcal6416a);
  554. val = gpio_get_value(gpio_pcal6416a);
  555. }
  556. else
  557. retval = gpio_direction_output(gpio_pcal6416a, val);
  558. if (retval)
  559. pr_err("%s: unable to set direction for gpio [%d]\n",
  560. __func__, gpio_pcal6416a);
  561. gpio_free(gpio_pcal6416a);
  562. }
  563. pr_info("pcal6416a mode set to dir[%c], offset[%d], val[%d]\n", in_out, off, val);
  564. return count;
  565. }
  566. static ssize_t show_pcal6416a_gpio_state(struct device *dev,
  567. struct device_attribute *devattr, char *buf)
  568. {
  569. struct pcal6416a_chip *chip = dev_get_drvdata(dev);
  570. struct pcal6416a_chip chip_state;
  571. int i, drv_str;
  572. uint16_t read_input;
  573. char *bufp = buf;
  574. pcal6416a_read_reg(chip, PCAL6416A_INPUT, &read_input);
  575. pcal6416a_read_reg(chip, PCAL6416A_DAT_OUT, &chip_state.reg_output);
  576. pcal6416a_read_reg(chip, PCAL6416A_CONFIG, &chip_state.reg_config);
  577. pcal6416a_read_reg(chip, PCAL6416A_DRIVE0, &chip_state.reg_drive0);
  578. pcal6416a_read_reg(chip, PCAL6416A_DRIVE1, &chip_state.reg_drive1);
  579. pcal6416a_read_reg(chip, PCAL6416A_EN_PULLUPDOWN,
  580. &chip_state.reg_enpullupdown);
  581. pcal6416a_read_reg(chip, PCAL6416A_SEL_PULLUPDOWN,
  582. &chip_state.reg_selpullupdown);
  583. for (i = 0; i < 16; i++) {
  584. bufp += sprintf(bufp, "Expander[3%02d]", i);
  585. if ((chip_state.reg_config>>i)&0x1)
  586. bufp += sprintf(bufp, " IN");
  587. else {
  588. if ((chip_state.reg_output>>i)&0x1)
  589. bufp += sprintf(bufp, " OUT_HIGH");
  590. else
  591. bufp += sprintf(bufp, " OUT_LOW");
  592. }
  593. if ((chip_state.reg_enpullupdown>>i)&0x1) {
  594. if ((chip_state.reg_selpullupdown>>i)&0x1)
  595. bufp += sprintf(bufp, " PULL_UP");
  596. else
  597. bufp += sprintf(bufp, " PULL_DOWN");
  598. } else
  599. bufp += sprintf(bufp, " PULL_NONE");
  600. if (i > 7)
  601. drv_str = (chip_state.reg_drive1>>((i-8)*2)) & 0x3;
  602. else
  603. drv_str = (chip_state.reg_drive0>>(i*2)) & 0x3;
  604. switch(drv_str) {
  605. case GPIO_CFG_6_25MA:
  606. bufp += sprintf(bufp, " DRV_6.25mA");
  607. break;
  608. case GPIO_CFG_12_5MA:
  609. bufp += sprintf(bufp, " DRV_12.5mA");
  610. break;
  611. case GPIO_CFG_18_75MA:
  612. bufp += sprintf(bufp, " DRV_18.75mA");
  613. break;
  614. case GPIO_CFG_25MA:
  615. bufp += sprintf(bufp, " DRV_25mA");
  616. break;
  617. }
  618. if ((read_input>>i)&0x1)
  619. bufp += sprintf(bufp, " VAL_HIGH\n");
  620. else
  621. bufp += sprintf(bufp, " VAL_LOW\n");
  622. }
  623. return strlen(buf);
  624. }
  625. static DEVICE_ATTR(expgpio, 0664,
  626. show_pcal6416a_gpio_state, store_pcal6416a_gpio_inout);
  627. #ifdef CONFIG_SEC_PM_DEBUG
  628. int expander_print_all(void)
  629. {
  630. struct pcal6416a_chip chip_state;
  631. int i, drv_str;
  632. uint16_t read_input;
  633. if (!g_dev)
  634. return -ENODEV;
  635. pcal6416a_read_reg(g_dev, PCAL6416A_INPUT, &read_input);
  636. pcal6416a_read_reg(g_dev, PCAL6416A_DAT_OUT, &chip_state.reg_output);
  637. pcal6416a_read_reg(g_dev, PCAL6416A_CONFIG, &chip_state.reg_config);
  638. pcal6416a_read_reg(g_dev, PCAL6416A_DRIVE0, &chip_state.reg_drive0);
  639. pcal6416a_read_reg(g_dev, PCAL6416A_DRIVE1, &chip_state.reg_drive1);
  640. pcal6416a_read_reg(g_dev, PCAL6416A_EN_PULLUPDOWN,
  641. &chip_state.reg_enpullupdown);
  642. pcal6416a_read_reg(g_dev, PCAL6416A_SEL_PULLUPDOWN,
  643. &chip_state.reg_selpullupdown);
  644. for (i = 0; i < 16; i++) {
  645. pr_cont("Expander[3%02d]", i);
  646. if ((chip_state.reg_config>>i)&0x1)
  647. pr_cont("\tIN");
  648. else {
  649. if ((chip_state.reg_output>>i)&0x1)
  650. pr_cont("\tOUT_HIGH");
  651. else
  652. pr_cont("\tOUT_LOW");
  653. }
  654. if ((chip_state.reg_enpullupdown>>i)&0x1) {
  655. if ((chip_state.reg_selpullupdown>>i)&0x1)
  656. pr_cont("\tPULL_UP");
  657. else
  658. pr_cont("\tPULL_DOWN");
  659. } else
  660. pr_cont("\tPULL_NONE");
  661. if (i > 7)
  662. drv_str = (chip_state.reg_drive1>>((i-8)*2)) & 0x3;
  663. else
  664. drv_str = (chip_state.reg_drive0>>(i*2)) & 0x3;
  665. switch (drv_str) {
  666. case GPIO_CFG_6_25MA:
  667. pr_cont("\tDRV_6.25mA");
  668. break;
  669. case GPIO_CFG_12_5MA:
  670. pr_cont("\tDRV_12.5mA");
  671. break;
  672. case GPIO_CFG_18_75MA:
  673. pr_cont("\tDRV_18.75mA");
  674. break;
  675. case GPIO_CFG_25MA:
  676. pr_cont("\tDRV_25mA");
  677. break;
  678. }
  679. if ((read_input>>i)&0x1)
  680. pr_cont("\tVAL_HIGH\n");
  681. else
  682. pr_cont("\tVAL_LOW\n");
  683. }
  684. return 0;
  685. }
  686. #endif
  687. static int expander_show(struct seq_file *s, void *unused)
  688. {
  689. struct pcal6416a_chip chip_state;
  690. int i, drv_str;
  691. uint16_t read_input;
  692. pcal6416a_read_reg(g_dev, PCAL6416A_INPUT, &read_input);
  693. pcal6416a_read_reg(g_dev, PCAL6416A_DAT_OUT, &chip_state.reg_output);
  694. pcal6416a_read_reg(g_dev, PCAL6416A_CONFIG, &chip_state.reg_config);
  695. pcal6416a_read_reg(g_dev, PCAL6416A_DRIVE0, &chip_state.reg_drive0);
  696. pcal6416a_read_reg(g_dev, PCAL6416A_DRIVE1, &chip_state.reg_drive1);
  697. pcal6416a_read_reg(g_dev, PCAL6416A_EN_PULLUPDOWN,
  698. &chip_state.reg_enpullupdown);
  699. pcal6416a_read_reg(g_dev, PCAL6416A_SEL_PULLUPDOWN,
  700. &chip_state.reg_selpullupdown);
  701. for (i = 0; i < 16; i++) {
  702. seq_printf(s, "Expander[3%02d]", i);
  703. if ((chip_state.reg_config>>i)&0x1)
  704. seq_printf(s, " IN");
  705. else {
  706. if ((chip_state.reg_output>>i)&0x1)
  707. seq_printf(s, " OUT_HIGH");
  708. else
  709. seq_printf(s, " OUT_LOW");
  710. }
  711. if ((chip_state.reg_enpullupdown>>i)&0x1) {
  712. if ((chip_state.reg_selpullupdown>>i)&0x1)
  713. seq_printf(s, " PULL_UP");
  714. else
  715. seq_printf(s, " PULL_DOWN");
  716. } else
  717. seq_printf(s, " PULL_NONE");
  718. if (i > 7)
  719. drv_str = (chip_state.reg_drive1>>((i-8)*2)) & 0x3;
  720. else
  721. drv_str = (chip_state.reg_drive0>>(i*2)) & 0x3;
  722. switch(drv_str) {
  723. case GPIO_CFG_6_25MA:
  724. seq_printf(s, " DRV_6.25mA");
  725. break;
  726. case GPIO_CFG_12_5MA:
  727. seq_printf(s, " DRV_12.5mA");
  728. break;
  729. case GPIO_CFG_18_75MA:
  730. seq_printf(s, " DRV_18.75mA");
  731. break;
  732. case GPIO_CFG_25MA:
  733. seq_printf(s, " DRV_25mA");
  734. break;
  735. }
  736. if ((read_input>>i)&0x1)
  737. seq_printf(s, " VAL_HIGH\n");
  738. else
  739. seq_printf(s, " VAL_LOW\n");
  740. }
  741. return 0;
  742. }
  743. static int expander_debug_open(struct inode *inode, struct file *file)
  744. {
  745. return single_open(file, expander_show, NULL);
  746. }
  747. static const struct file_operations expander_operations = {
  748. .open = expander_debug_open,
  749. .read = seq_read,
  750. .llseek = seq_lseek,
  751. .release = single_release,
  752. };
  753. static int __devinit pcal6416a_gpio_probe(struct i2c_client *client,
  754. const struct i2c_device_id *id)
  755. {
  756. struct device_node *np = client->dev.of_node;
  757. struct pcal6416a_platform_data *pdata = NULL;
  758. struct pcal6416a_chip *dev;
  759. struct gpio_chip *gc;
  760. struct dentry *debugfs_file;
  761. int ret;
  762. int retry;
  763. pr_info("[%s]\n", __func__);
  764. if (!i2c_check_functionality(client->adapter,
  765. I2C_FUNC_SMBUS_BYTE_DATA)) {
  766. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  767. return -EIO;
  768. }
  769. #ifdef CONFIG_OF
  770. if (np) {
  771. pdata = devm_kzalloc(&client->dev,
  772. sizeof(struct pcal6416a_platform_data),
  773. GFP_KERNEL);
  774. if (!pdata) {
  775. dev_err(&client->dev, "Failed to allocate memory\n");
  776. return -ENOMEM;
  777. }
  778. ret = pcal6416a_parse_dt(&client->dev, pdata);
  779. if (ret) {
  780. pr_err("[%s] pcal6416a parse dt failed\n", __func__);
  781. return ret;
  782. }
  783. } else {
  784. pdata = client->dev.platform_data;
  785. pr_info("GPIO Expender failed to align dtsi %s",
  786. __func__);
  787. }
  788. #else
  789. pdata = client->dev.platform_data;
  790. #endif
  791. if (pdata == NULL) {
  792. dev_err(&client->dev, "missing platform data\n");
  793. return -ENODEV;
  794. }
  795. pcal6416a_power_ctrl(pdata, POWER_ON);
  796. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  797. if (dev == NULL) {
  798. dev_err(&client->dev, "failed to alloc memory\n");
  799. return -ENOMEM;
  800. }
  801. dev->client = client;
  802. dev->pdata = pdata;
  803. dev->gpio_start = pdata->gpio_start;
  804. gc = &dev->gpio_chip;
  805. gc->direction_input = pcal6416a_gpio_direction_input;
  806. gc->direction_output = pcal6416a_gpio_direction_output;
  807. gc->get = pcal6416a_gpio_get_value;
  808. gc->set = pcal6416a_gpio_set_value;
  809. gc->request = pcal6416a_gpio_request;
  810. gc->free = pcal6416a_gpio_free;
  811. gc->can_sleep = 0;
  812. /*Require dev to use the of_gpio api*/
  813. gc->dev = &client->dev;
  814. gc->base = pdata->gpio_start;
  815. gc->ngpio = pdata->ngpio;
  816. gc->label = client->name;
  817. gc->owner = THIS_MODULE;
  818. mutex_init(&dev->lock);
  819. dev->reg_config = pdata->init_config;
  820. dev->reg_output = pdata->init_data_out;
  821. dev->reg_enpullupdown = pdata->init_en_pull;
  822. dev->reg_selpullupdown = pdata->init_sel_pull;
  823. retry = 0;
  824. while (1) {
  825. ret = pcal6416a_reset_chip(pdata);
  826. if (ret) {
  827. pr_err("[%s]reset control fail\n", __func__);
  828. } else {
  829. ret = pcal6416a_gpio_setup(dev);
  830. if (ret) {
  831. dev_err(&client->dev,
  832. "expander setup i2c retry [%d]\n", retry);
  833. } else {
  834. pr_info("[%s]Expander setup success [%d]\n",
  835. __func__, retry);
  836. break;
  837. }
  838. }
  839. if (retry++ > 5) {
  840. dev_err(&client->dev,
  841. "Failed to expander retry[%d]\n", retry);
  842. WARN_ON(ret);
  843. goto err;
  844. }
  845. usleep_range(100, 200);
  846. }
  847. ret = gpiochip_add(&dev->gpio_chip);
  848. if (ret)
  849. goto err;
  850. dev_info(&client->dev, "gpios %d..%d on a %s\n",
  851. gc->base, gc->base + gc->ngpio - 1,
  852. client->name);
  853. pcal6416a_dev = device_create(sec_class, NULL, 0, dev, "expander");
  854. if (IS_ERR(pcal6416a_dev)) {
  855. dev_err(&client->dev,
  856. "Failed to create device for expander\n");
  857. ret = -ENODEV;
  858. goto err;
  859. }
  860. ret = sysfs_create_file(&pcal6416a_dev->kobj, &dev_attr_expgpio.attr);
  861. if (ret) {
  862. dev_err(&client->dev,
  863. "Failed to create sysfs group for expander\n");
  864. goto err_destroy;
  865. }
  866. dev->dentry = debugfs_create_dir("expander", NULL);
  867. if (IS_ERR_OR_NULL(dev->dentry)) {
  868. dev_err(&client->dev,
  869. "Failed to create debugfs dir for expander\n");
  870. goto err_debug_dir;
  871. }
  872. debugfs_file = debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  873. dev->dentry, NULL, &expander_operations);
  874. if (IS_ERR_OR_NULL(debugfs_file)) {
  875. dev_err(&client->dev,
  876. "Failed to create debugfs file for gpio\n");
  877. goto err_debug_file;
  878. }
  879. i2c_set_clientdata(client, dev);
  880. g_dev = dev;
  881. return 0;
  882. err_debug_file:
  883. debugfs_remove_recursive(dev->dentry);
  884. err_debug_dir:
  885. sysfs_remove_file(&pcal6416a_dev->kobj, &dev_attr_expgpio.attr);
  886. err_destroy:
  887. device_destroy(sec_class, 0);
  888. err:
  889. pcal6416a_power_ctrl(pdata, POWER_OFF);
  890. mutex_destroy(&dev->lock);
  891. kfree(dev);
  892. return ret;
  893. }
  894. static int __devexit pcal6416a_gpio_remove(struct i2c_client *client)
  895. {
  896. struct pcal6416a_chip *dev = i2c_get_clientdata(client);
  897. int ret;
  898. pcal6416a_power_ctrl(dev->pdata, POWER_OFF);
  899. ret = gpiochip_remove(&dev->gpio_chip);
  900. if (ret) {
  901. dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
  902. return ret;
  903. }
  904. if (!IS_ERR_OR_NULL(dev->dentry))
  905. debugfs_remove_recursive(dev->dentry);
  906. sysfs_remove_file(&pcal6416a_dev->kobj, &dev_attr_expgpio.attr);
  907. mutex_destroy(&dev->lock);
  908. kfree(dev);
  909. return 0;
  910. }
  911. #ifdef CONFIG_OF
  912. static struct of_device_id pcal6416a_dt_ids[] = {
  913. { .compatible = "pcal6416a,gpio-expander",},
  914. };
  915. #endif
  916. static const struct i2c_device_id pcal6416a_gpio_id[] = {
  917. {DRV_NAME, 0, },
  918. { }
  919. };
  920. MODULE_DEVICE_TABLE(i2c, pcal6416a_gpio_id);
  921. static struct i2c_driver pcal6416a_gpio_driver = {
  922. .driver = {
  923. .name = DRV_NAME,
  924. #ifdef CONFIG_OF
  925. .of_match_table = of_match_ptr(pcal6416a_dt_ids),
  926. #endif
  927. },
  928. .probe = pcal6416a_gpio_probe,
  929. .remove = __devexit_p(pcal6416a_gpio_remove),
  930. .id_table = pcal6416a_gpio_id,
  931. };
  932. static int __init pcal6416a_gpio_init(void)
  933. {
  934. return i2c_add_driver(&pcal6416a_gpio_driver);
  935. }
  936. module_init(pcal6416a_gpio_init);
  937. static void __exit pcal6416a_gpio_exit(void)
  938. {
  939. i2c_del_driver(&pcal6416a_gpio_driver);
  940. }
  941. module_exit(pcal6416a_gpio_exit);
  942. MODULE_DESCRIPTION("GPIO expander driver for PCAL6416A");
  943. MODULE_LICENSE("GPL");