gpio-mc5587.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * GPIO Chip driver for Analog Devices
  3. * MC5587 I/O Expander and QWERTY Keypad Controller
  4. *
  5. * Copyright 2009-2010 Analog Devices Inc.
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/i2c.h>
  14. #include <linux/gpio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/i2c/mc5587.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/regulator/consumer.h>
  22. #if defined(CONFIG_OF)
  23. #include <linux/of_device.h>
  24. #include <linux/of_gpio.h>
  25. #endif
  26. #define DRV_NAME "mc5587-gpio"
  27. /*
  28. * Early pre 4.0 Silicon required to delay readout by at least 25ms,
  29. * since the Event Counter Register updated 25ms after the interrupt
  30. * asserted.
  31. */
  32. #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
  33. struct mc5587_gpio {
  34. struct i2c_client *client;
  35. struct gpio_chip gpio_chip;
  36. struct mutex lock; /* protect cached dir, dat_out */
  37. /* protect serialized access to the interrupt controller bus */
  38. struct mutex irq_lock;
  39. unsigned gpio_start;
  40. int irq;
  41. int irq_base;
  42. int irq_gpio;
  43. uint8_t dat_out[3];
  44. uint8_t dir[3];
  45. uint8_t int_lvl[3];
  46. uint8_t int_en[3];
  47. uint8_t irq_mask[3];
  48. uint8_t irq_stat[3];
  49. };
  50. static int mc5587_read(struct i2c_client *client, u8 reg)
  51. {
  52. int ret = i2c_smbus_read_byte_data(client, reg);
  53. if (ret < 0)
  54. dev_err(&client->dev, "Read Error [%d]\n", ret);
  55. return ret;
  56. }
  57. static int mc5587_write(struct i2c_client *client, u8 reg, u8 val)
  58. {
  59. int ret = i2c_smbus_write_byte_data(client, reg, val);
  60. if (ret < 0)
  61. dev_err(&client->dev, "Write Error [%d]\n", ret);
  62. return ret;
  63. }
  64. static int mc5587_gpio_get_value(struct gpio_chip *chip, unsigned off)
  65. {
  66. struct mc5587_gpio *dev =
  67. container_of(chip, struct mc5587_gpio, gpio_chip);
  68. return !!(mc5587_read(dev->client,
  69. GPIDAT1 + MC5587_BANK(off)) & MC5587_BIT(off));
  70. }
  71. static void mc5587_gpio_set_value(struct gpio_chip *chip,
  72. unsigned off, int val)
  73. {
  74. unsigned bank, bit;
  75. struct mc5587_gpio *dev =
  76. container_of(chip, struct mc5587_gpio, gpio_chip);
  77. pr_info("%s\n", __func__);
  78. bank = MC5587_BANK(off);
  79. bit = MC5587_BIT(off);
  80. mutex_lock(&dev->lock);
  81. if (val)
  82. dev->dat_out[bank] |= bit;
  83. else
  84. dev->dat_out[bank] &= ~bit;
  85. mc5587_write(dev->client, GPODAT1 + bank,
  86. dev->dat_out[bank]);
  87. mutex_unlock(&dev->lock);
  88. }
  89. static int mc5587_gpio_direction_input(struct gpio_chip *chip, unsigned off)
  90. {
  91. int ret;
  92. unsigned bank;
  93. struct mc5587_gpio *dev =
  94. container_of(chip, struct mc5587_gpio, gpio_chip);
  95. pr_info("%s\n", __func__);
  96. bank = MC5587_BANK(off);
  97. mutex_lock(&dev->lock);
  98. dev->dir[bank] &= ~MC5587_BIT(off);
  99. ret = mc5587_write(dev->client, GPIODIR1 + bank, dev->dir[bank]);
  100. mutex_unlock(&dev->lock);
  101. return ret;
  102. }
  103. static int mc5587_gpio_direction_output(struct gpio_chip *chip,
  104. unsigned off, int val)
  105. {
  106. int ret;
  107. unsigned bank, bit;
  108. struct mc5587_gpio *dev =
  109. container_of(chip, struct mc5587_gpio, gpio_chip);
  110. pr_info("%s\n", __func__);
  111. bank = MC5587_BANK(off);
  112. bit = MC5587_BIT(off);
  113. mutex_lock(&dev->lock);
  114. dev->dir[bank] |= bit;
  115. if (val)
  116. dev->dat_out[bank] |= bit;
  117. else
  118. dev->dat_out[bank] &= ~bit;
  119. ret = mc5587_write(dev->client, GPODAT1 + bank,
  120. dev->dat_out[bank]);
  121. ret |= mc5587_write(dev->client, GPIODIR1 + bank,
  122. dev->dir[bank]);
  123. mutex_unlock(&dev->lock);
  124. return ret;
  125. }
  126. static int mc5587_gpio_request(struct gpio_chip *chip, unsigned off)
  127. {
  128. struct mc5587_gpio *dev =
  129. container_of(chip, struct mc5587_gpio, gpio_chip);
  130. pr_info("%s\n", __func__);
  131. if (off >= dev->gpio_chip.ngpio) {
  132. pr_err("[%s] offset over max = [%d]\n", __func__, off);
  133. return 1;
  134. }
  135. /* to do*/
  136. return 0;
  137. }
  138. static void mc5587_gpio_free(struct gpio_chip *chip, unsigned off)
  139. {
  140. struct mc5587_gpio *dev =
  141. container_of(chip, struct mc5587_gpio, gpio_chip);
  142. pr_info("%s\n", __func__);
  143. if (off >= dev->gpio_chip.ngpio) {
  144. pr_err("[%s] offset over max = [%d]\n", __func__, off);
  145. }
  146. /* to do*/
  147. }
  148. #ifdef CONFIG_GPIO_MC5587_IRQ
  149. static int mc5587_gpio_to_irq(struct gpio_chip *chip, unsigned off)
  150. {
  151. struct mc5587_gpio *dev =
  152. container_of(chip, struct mc5587_gpio, gpio_chip);
  153. return dev->irq_base + off;
  154. }
  155. static void mc5587_irq_bus_lock(struct irq_data *d)
  156. {
  157. struct mc5587_gpio *dev = irq_data_get_irq_chip_data(d);
  158. mutex_lock(&dev->irq_lock);
  159. }
  160. /*
  161. * genirq core code can issue chip->mask/unmask from atomic context.
  162. * This doesn't work for slow busses where an access needs to sleep.
  163. * bus_sync_unlock() is therefore called outside the atomic context,
  164. * syncs the current irq mask state with the slow external controller
  165. * and unlocks the bus.
  166. */
  167. static void mc5587_irq_bus_sync_unlock(struct irq_data *d)
  168. {
  169. struct mc5587_gpio *dev = irq_data_get_irq_chip_data(d);
  170. int i;
  171. for (i = 0; i <= MC5587_BANK(MC5587_MAXGPIO); i++)
  172. if (dev->int_en[i] ^ dev->irq_mask[i]) {
  173. dev->int_en[i] = dev->irq_mask[i];
  174. mc5587_write(dev->client, GPINTEN1 + i,
  175. dev->int_en[i]);
  176. }
  177. mutex_unlock(&dev->irq_lock);
  178. }
  179. static void mc5587_irq_mask(struct irq_data *d)
  180. {
  181. struct mc5587_gpio *dev = irq_data_get_irq_chip_data(d);
  182. unsigned gpio = d->irq - dev->irq_base;
  183. dev->irq_mask[MC5587_BANK(gpio)] &= ~MC5587_BIT(gpio);
  184. }
  185. static void mc5587_irq_unmask(struct irq_data *d)
  186. {
  187. struct mc5587_gpio *dev = irq_data_get_irq_chip_data(d);
  188. unsigned gpio = d->irq - dev->irq_base;
  189. dev->irq_mask[MC5587_BANK(gpio)] |= MC5587_BIT(gpio);
  190. }
  191. static int mc5587_irq_set_type(struct irq_data *d, unsigned int type)
  192. {
  193. struct mc5587_gpio *dev = irq_data_get_irq_chip_data(d);
  194. uint16_t gpio = d->irq - dev->irq_base;
  195. unsigned bank, bit;
  196. if ((type & IRQ_TYPE_EDGE_BOTH)) {
  197. dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
  198. d->irq, type);
  199. return -EINVAL;
  200. }
  201. bank = MC5587_BANK(gpio);
  202. bit = MC5587_BIT(gpio);
  203. if (type & IRQ_TYPE_LEVEL_HIGH)
  204. dev->int_lvl[bank] |= bit;
  205. else if (type & IRQ_TYPE_LEVEL_LOW)
  206. dev->int_lvl[bank] &= ~bit;
  207. else
  208. return -EINVAL;
  209. mc5587_gpio_direction_input(&dev->gpio_chip, gpio);
  210. mc5587_write(dev->client, GPINTLVL1 + bank,
  211. dev->int_lvl[bank]);
  212. return 0;
  213. }
  214. static struct irq_chip mc5587_irq_chip = {
  215. .name = "mc5587",
  216. .irq_mask = mc5587_irq_mask,
  217. .irq_unmask = mc5587_irq_unmask,
  218. .irq_bus_lock = mc5587_irq_bus_lock,
  219. .irq_bus_sync_unlock = mc5587_irq_bus_sync_unlock,
  220. .irq_set_type = mc5587_irq_set_type,
  221. };
  222. static int mc5587_read_intstat(struct i2c_client *client, u8 *buf)
  223. {
  224. int ret = i2c_smbus_read_i2c_block_data(client, GPINTST1, 3, buf);
  225. if (ret < 0)
  226. dev_err(&client->dev, "Read INT_STAT Error\n");
  227. return ret;
  228. }
  229. static irqreturn_t mc5587_irq_handler(int irq, void *devid)
  230. {
  231. struct mc5587_gpio *dev = devid;
  232. unsigned status, bank, bit, pending;
  233. int ret;
  234. status = mc5587_read(dev->client, INTPND);
  235. if (status & MC5587_GPIPND) {
  236. ret = mc5587_read_intstat(dev->client, dev->irq_stat);
  237. if (ret < 0)
  238. memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
  239. for (bank = 0; bank <= MC5587_BANK(MC5587_MAXGPIO);
  240. bank++, bit = 0) {
  241. pending = dev->irq_stat[bank] & dev->irq_mask[bank];
  242. while (pending) {
  243. if (pending & (1 << bit)) {
  244. handle_nested_irq(dev->irq_base +
  245. (bank << 3) + bit);
  246. pending &= ~(1 << bit);
  247. }
  248. bit++;
  249. }
  250. }
  251. }
  252. mc5587_write(dev->client, INTPND, status); /* Status is W1C */
  253. return IRQ_HANDLED;
  254. }
  255. static int mc5587_irq_setup(struct mc5587_gpio *dev)
  256. {
  257. struct i2c_client *client = dev->client;
  258. struct mc5587_gpio_platform_data *pdata = client->dev.platform_data;
  259. unsigned gpio;
  260. int ret, cur_irq;
  261. mc5587_write(client, CFGREG, MC5587_AUTOINC);
  262. mc5587_write(client, INTPND, -1); /* status is W1C */
  263. mc5587_read_intstat(client, dev->irq_stat); /* read to clear */
  264. dev->irq_gpio = pdata->irq_gpio;
  265. dev->irq_base = pdata->irq_base;
  266. mutex_init(&dev->irq_lock);
  267. dev->irq = gpio_to_irq(dev->irq_gpio);
  268. client->irq = dev->irq;
  269. pr_info("[%s] dev irq=[%d], irq_gpio=[%d] irq_base=[%d]\n",
  270. __func__, dev->irq, dev->irq_gpio, dev->irq_base);
  271. ret = gpio_request(dev->irq_gpio, "mc5587_expander_irq");
  272. if (ret) {
  273. dev_err(&client->dev, "[%s] failed requesting gpio %d\n",
  274. __func__, dev->irq_gpio);
  275. return ret;
  276. }
  277. gpio_direction_input(dev->irq_gpio);
  278. gpio_free(dev->irq_gpio);
  279. for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
  280. cur_irq = gpio + dev->irq_base;
  281. irq_set_chip_data(cur_irq, dev);
  282. irq_set_chip_and_handler(cur_irq, &mc5587_irq_chip,
  283. handle_level_irq);
  284. irq_set_nested_thread(cur_irq, 1);
  285. #ifdef CONFIG_ARM
  286. set_irq_flags(cur_irq, IRQF_VALID);
  287. #else
  288. irq_set_noprobe(irq);
  289. #endif
  290. }
  291. ret = request_threaded_irq(dev->irq,
  292. NULL, mc5587_irq_handler,
  293. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  294. "mc5587-irq", dev);
  295. if (ret) {
  296. dev_err(&client->dev, "failed to request irq %d\n",
  297. client->irq);
  298. goto out;
  299. }
  300. mc5587_write(client, CFGREG,
  301. MC5587_AUTOINC | MC5587_INTCFG | MC5587_GPIIEN);
  302. return 0;
  303. out:
  304. dev->irq_base = 0;
  305. return ret;
  306. }
  307. static void mc5587_irq_teardown(struct mc5587_gpio *dev)
  308. {
  309. if (dev->irq_base)
  310. free_irq(dev->client->irq, dev);
  311. }
  312. #else
  313. static int mc5587_irq_setup(struct mc5587_gpio *dev)
  314. {
  315. struct i2c_client *client = dev->client;
  316. dev_warn(&client->dev, "interrupt support not compiled in\n");
  317. return 0;
  318. }
  319. static void mc5587_irq_teardown(struct mc5587_gpio *dev)
  320. {
  321. }
  322. #endif /* CONFIG_GPIO_MC5587_IRQ */
  323. static int __devinit mc5587_gpio_setup(struct mc5587_gpio *dev)
  324. {
  325. int ret = 0;
  326. unsigned bank = 0, bit = 0x00;
  327. ret |= mc5587_write(dev->client, CFGREG,
  328. MC5587_AUTOINC | MC5587_GPIIEN);
  329. ret |= mc5587_write(dev->client, INTPND, MC5587_GPIPND);
  330. ret |= mc5587_write(dev->client, KPMSEL1, 0x00);
  331. ret |= mc5587_write(dev->client, KPMSEL2, 0x00);
  332. ret |= mc5587_write(dev->client, KPMSEL3, 0x00);
  333. ret |= mc5587_write(dev->client, GPINTEN1, 0x00);
  334. ret |= mc5587_write(dev->client, GPINTEN2,
  335. MC5587_BIT(GPI_PIN_CAM_SENSOR_DET)
  336. | MC5587_BIT(GPI_PIN_TX_GTR_THRES));
  337. ret |= mc5587_write(dev->client, GPINTEN3, 0x00);
  338. ret |= mc5587_write(dev->client, GPINTLVL2,
  339. MC5587_BIT(GPI_PIN_CAM_SENSOR_DET)
  340. | MC5587_BIT(GPI_PIN_TX_GTR_THRES));
  341. ret |= mc5587_write(dev->client, GPIEMSEL1, 0x00);
  342. ret |= mc5587_write(dev->client, GPIEMSEL2, 0x00);
  343. ret |= mc5587_write(dev->client, GPIEMSEL3, 0x00);
  344. ret |= mc5587_write(dev->client, DBNCON1, 0xFF);
  345. ret |= mc5587_write(dev->client, DBNCON2, 0xFF);
  346. ret |= mc5587_write(dev->client, DBNCON3, 0xFF);
  347. bank = 0;
  348. bit = 0x01; /* 0b00000001 */
  349. dev->dir[bank] = bit;
  350. dev->dat_out[bank] = 0x00;
  351. ret |= mc5587_write(dev->client, GPIODIR1, dev->dir[bank]);
  352. ret |= mc5587_write(dev->client, GPODAT1, dev->dat_out[bank]);
  353. bank = 1;
  354. bit = 0xed; /* 0b11101101 */
  355. dev->dir[bank] = bit;
  356. dev->dat_out[bank] = 0x00;
  357. ret |= mc5587_write(dev->client, GPIODIR2, dev->dir[bank]);
  358. ret |= mc5587_write(dev->client, GPODAT2, dev->dat_out[bank]);
  359. bank = 2;
  360. bit = 0x03; /* 0b00000011 */
  361. dev->dir[bank] = bit;
  362. dev->dat_out[bank] = 0x00;
  363. ret |= mc5587_write(dev->client, GPIODIR3, dev->dir[bank]);
  364. ret |= mc5587_write(dev->client, GPODAT3, dev->dat_out[bank]);
  365. mc5587_read(dev->client, GPINTST1); /* clear */
  366. mc5587_read(dev->client, GPINTST2);
  367. mc5587_read(dev->client, GPINTST3);
  368. pr_info("[%s] GPIO Expander Init setting\n", __func__);
  369. return ret;
  370. }
  371. #ifdef CONFIG_OF
  372. static int mc5587_parse_dt(struct device *dev,
  373. struct mc5587_gpio_platform_data *pdata)
  374. {
  375. struct device_node *np = dev->of_node;
  376. int ret;
  377. ret = of_property_read_u32(np, "mc5587,gpio_start", &pdata->gpio_start);
  378. if (ret < 0) {
  379. pr_err("[%s]: Unable to read mc5587,gpio_start\n", __func__);
  380. return ret;
  381. }
  382. ret = of_property_read_u32(np, "mc5587,ngpio", &pdata->ngpio);
  383. if (ret < 0) {
  384. pr_err("[%s]: Unable to read mc5587,ngpio\n", __func__);
  385. return ret;
  386. }
  387. ret = of_property_read_u32(np, "mc5587,irq-base", &pdata->irq_base);
  388. if (ret < 0) {
  389. pr_err("[%s]: Unable to read mc5587,irq-base\n", __func__);
  390. return ret;
  391. }
  392. pdata->reset_gpio = of_get_named_gpio(np, "mc5587,reset-gpio", 0);
  393. pdata->irq_gpio = of_get_named_gpio(np, "mc5587,irq-gpio", 0);
  394. dev->platform_data = pdata;
  395. pr_info("[%s] gpio_start=[%d] ngpio=[%d] reset_gpio=[%d] irq_gpio=[%d]\n",
  396. __func__, pdata->gpio_start, pdata->ngpio,
  397. pdata->reset_gpio, pdata->irq_gpio);
  398. return 0;
  399. }
  400. #endif
  401. static void mc5587_power_ctrl(bool enable)
  402. {
  403. int ret = 0;
  404. static struct regulator *reg_s4;
  405. if (!reg_s4) {
  406. reg_s4 = regulator_get(NULL, "8084_s4");
  407. if (IS_ERR(reg_s4)) {
  408. pr_err("%s: could not get 8917_s4, rc = %ld\n",
  409. __func__, PTR_ERR(reg_s4));
  410. return;
  411. }
  412. ret = regulator_set_voltage(reg_s4, 1800000, 1800000);
  413. if (ret) {
  414. pr_err("%s: unable to set s4 voltage to 1.8V\n",
  415. __func__);
  416. return;
  417. }
  418. }
  419. if (enable) {
  420. if (regulator_is_enabled(reg_s4))
  421. pr_err("%s: S4(1.8V) is enabled\n", __func__);
  422. else
  423. ret = regulator_enable(reg_s4);
  424. if (ret) {
  425. pr_err("%s: enable s4 failed, rc=%d\n",
  426. __func__, ret);
  427. return;
  428. }
  429. pr_info("%s: gpio expander 1.8V on is finished.\n", __func__);
  430. } else {
  431. if (regulator_is_enabled(reg_s4))
  432. ret = regulator_disable(reg_s4);
  433. else
  434. pr_err("%s: S4(1.8V) is disabled\n", __func__);
  435. if (ret) {
  436. pr_err("%s: disable S4 failed, rc=%d\n",
  437. __func__, ret);
  438. return;
  439. }
  440. pr_info("%s: gpio expander 1.8V off is finished.\n", __func__);
  441. }
  442. pr_err("[mc5587 gpio expander] %s enable(%d)\n", __func__, enable);
  443. return;
  444. }
  445. static int mc5587_reset_chip(struct mc5587_gpio_platform_data *pdata)
  446. {
  447. int retval;
  448. int reset_mc5587 = pdata->reset_gpio;
  449. if (gpio_is_valid(reset_mc5587)) {
  450. retval = gpio_request(reset_mc5587,
  451. "mc5587_reset_gpio");
  452. if (retval) {
  453. pr_err("%s: unable to request gpio [%d]\n",
  454. __func__, reset_mc5587);
  455. return retval;
  456. }
  457. retval = gpio_direction_output(reset_mc5587, 1);
  458. if (retval) {
  459. pr_err("%s: unable to set direction for gpio [%d]\n",
  460. __func__, reset_mc5587);
  461. return retval;
  462. }
  463. usleep(100);
  464. gpio_set_value(reset_mc5587, 0);
  465. usleep(100);
  466. gpio_set_value(reset_mc5587, 1);
  467. pr_info("%s: gpio expander reset.\n", __func__);
  468. gpio_free(reset_mc5587);
  469. return 0;
  470. } else
  471. pr_err("%s: reset_mc5587 fail\n", __func__);
  472. return 0;
  473. }
  474. struct device *gpio_dev;
  475. extern struct class *sec_class;
  476. static ssize_t store_mc5587_gpio_inout(struct device *dev,
  477. struct device_attribute *devattr,
  478. const char *buf, size_t count)
  479. {
  480. int retval, off, val, gpio_mc5587;
  481. char in_out, msg[13];
  482. struct mc5587_gpio *data = dev_get_drvdata(dev);
  483. retval = sscanf(buf, "%c %d %d", &in_out, &off, &val);
  484. if (retval == 0) {
  485. dev_err(&data->client->dev, "[%s] fail to mc5587 out.\n", __func__);
  486. return count;
  487. }
  488. gpio_mc5587 = 300 + off;
  489. sprintf(msg, "exp-gpio%d\n", off);
  490. if (gpio_is_valid(gpio_mc5587)) {
  491. retval = gpio_request(gpio_mc5587, msg);
  492. if (retval) {
  493. pr_err("[%s] unable to request gpio=[%d] err=[%d]\n",
  494. __func__, gpio_mc5587, retval);
  495. return count;
  496. }
  497. if (in_out == 'i') {
  498. retval = gpio_direction_input(gpio_mc5587);
  499. val = gpio_get_value(gpio_mc5587);
  500. }
  501. else
  502. retval = gpio_direction_output(gpio_mc5587, val);
  503. if (retval)
  504. pr_err("%s: unable to set direction for gpio [%d]\n",
  505. __func__, gpio_mc5587);
  506. gpio_free(gpio_mc5587);
  507. }
  508. pr_info("mc5587 mode set to dir[%c], offset[%d], val[%d]\n", in_out, off, val);
  509. return count;
  510. }
  511. static DEVICE_ATTR(expgpio, 0664, NULL, store_mc5587_gpio_inout);
  512. static int __devinit mc5587_gpio_probe(struct i2c_client *client,
  513. const struct i2c_device_id *id)
  514. {
  515. struct device_node *np = client->dev.of_node;
  516. struct mc5587_gpio_platform_data *pdata = NULL;
  517. struct mc5587_gpio *dev;
  518. struct gpio_chip *gc;
  519. int ret, i, revid, retval;
  520. if (!i2c_check_functionality(client->adapter,
  521. I2C_FUNC_SMBUS_BYTE_DATA)) {
  522. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  523. return -EIO;
  524. }
  525. #ifdef CONFIG_OF
  526. if (np) {
  527. pdata = devm_kzalloc(&client->dev,
  528. sizeof(struct mc5587_gpio_platform_data),
  529. GFP_KERNEL);
  530. if (!pdata) {
  531. dev_err(&client->dev, "Failed to allocate memory\n");
  532. return -ENOMEM;
  533. }
  534. retval = mc5587_parse_dt(&client->dev, pdata);
  535. if (retval) {
  536. pr_err("[%s] mc5587 parse dt failed\n", __func__);
  537. return retval;
  538. }
  539. mc5587_power_ctrl(true);
  540. mc5587_reset_chip(pdata);
  541. } else {
  542. pdata = client->dev.platform_data;
  543. pr_info("GPIO Expender failed to align dtsi %s",
  544. __func__);
  545. }
  546. #else
  547. pdata = client->dev.platform_data;
  548. #endif
  549. if (pdata == NULL) {
  550. dev_err(&client->dev, "missing platform data\n");
  551. return -ENODEV;
  552. }
  553. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  554. if (dev == NULL) {
  555. dev_err(&client->dev, "failed to alloc memory\n");
  556. return -ENOMEM;
  557. }
  558. dev->client = client;
  559. gc = &dev->gpio_chip;
  560. gc->direction_input = mc5587_gpio_direction_input;
  561. gc->direction_output = mc5587_gpio_direction_output;
  562. gc->get = mc5587_gpio_get_value;
  563. gc->set = mc5587_gpio_set_value;
  564. gc->request = mc5587_gpio_request;
  565. gc->free = mc5587_gpio_free;
  566. gc->can_sleep = 0;
  567. /*Require dev to use the of_gpio api*/
  568. gc->dev = &client->dev;
  569. #ifdef CONFIG_GPIO_MC5587_IRQ
  570. gc->to_irq = mc5587_gpio_to_irq;
  571. #endif
  572. gc->base = pdata->gpio_start;
  573. gc->ngpio = pdata->ngpio;
  574. gc->label = client->name;
  575. gc->owner = THIS_MODULE;
  576. mutex_init(&dev->lock);
  577. ret = mc5587_read(dev->client, DEVID);
  578. if (ret < 0)
  579. goto err;
  580. revid = ret & MC5587_DEVICE_ID_MASK;
  581. for (i = 0, ret = 0; i <= MC5587_BANK(MC5587_MAXGPIO); i++) {
  582. dev->dat_out[i] = mc5587_read(client, GPODAT1 + i);
  583. dev->dir[i] = mc5587_read(client, GPIODIR1 + i);
  584. ret |= mc5587_write(client, KPMSEL1 + i, 0);
  585. ret |= mc5587_write(client, GPIPU1 + i,
  586. (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
  587. ret |= mc5587_write(client, GPINTEN1 + i, 0);
  588. if (ret)
  589. goto err;
  590. }
  591. mc5587_gpio_setup(dev);
  592. if (pdata->irq_base) {
  593. if (WA_DELAYED_READOUT_REVID(revid)) {
  594. dev_warn(&client->dev, "GPIO int not supported\n");
  595. } else {
  596. ret = mc5587_irq_setup(dev);
  597. if (ret)
  598. goto err;
  599. }
  600. }
  601. ret = gpiochip_add(&dev->gpio_chip);
  602. if (ret)
  603. goto err_irq;
  604. dev_info(&client->dev, "gpios %d..%d (IRQ Base %d) on a %s Rev. %d\n",
  605. gc->base, gc->base + gc->ngpio - 1,
  606. pdata->irq_base, client->name, revid);
  607. if (pdata->setup) {
  608. ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
  609. if (ret < 0)
  610. dev_warn(&client->dev, "setup failed, %d\n", ret);
  611. } else {
  612. pr_info("[%s] No pdata->setup\n", __func__);
  613. }
  614. gpio_dev = device_create(sec_class, NULL, 0, dev, "expander");
  615. if (IS_ERR(gpio_dev)) {
  616. dev_err(&client->dev,
  617. "Failed to create device for expander\n");
  618. ret = -ENODEV;
  619. goto err;
  620. }
  621. ret = sysfs_create_file(&gpio_dev->kobj, &dev_attr_expgpio.attr);
  622. if (ret) {
  623. dev_err(&client->dev,
  624. "Failed to create sysfs group for expander\n");
  625. goto err_destroy;
  626. }
  627. i2c_set_clientdata(client, dev);
  628. return 0;
  629. err_irq:
  630. mc5587_irq_teardown(dev);
  631. err_destroy:
  632. device_destroy(sec_class, 0);
  633. err:
  634. kfree(dev);
  635. return ret;
  636. }
  637. static int __devexit mc5587_gpio_remove(struct i2c_client *client)
  638. {
  639. struct mc5587_gpio_platform_data *pdata = client->dev.platform_data;
  640. struct mc5587_gpio *dev = i2c_get_clientdata(client);
  641. int ret;
  642. if (pdata->teardown) {
  643. ret = pdata->teardown(client,
  644. dev->gpio_chip.base, dev->gpio_chip.ngpio,
  645. pdata->context);
  646. if (ret < 0) {
  647. dev_err(&client->dev, "teardown failed %d\n", ret);
  648. return ret;
  649. }
  650. }
  651. if (dev->irq_base)
  652. free_irq(dev->client->irq, dev);
  653. ret = gpiochip_remove(&dev->gpio_chip);
  654. if (ret) {
  655. dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
  656. return ret;
  657. }
  658. kfree(dev);
  659. return 0;
  660. }
  661. #ifdef CONFIG_OF
  662. static struct of_device_id mc5587_dt_ids[] = {
  663. { .compatible = "mc5587,gpio-expander",},
  664. };
  665. #endif
  666. static const struct i2c_device_id mc5587_gpio_id[] = {
  667. {DRV_NAME, 0},
  668. {}
  669. };
  670. MODULE_DEVICE_TABLE(i2c, mc5587_gpio_id);
  671. static struct i2c_driver mc5587_gpio_driver = {
  672. .driver = {
  673. .name = DRV_NAME,
  674. #ifdef CONFIG_OF
  675. .of_match_table = of_match_ptr(mc5587_dt_ids),
  676. #endif
  677. },
  678. .probe = mc5587_gpio_probe,
  679. .remove = __devexit_p(mc5587_gpio_remove),
  680. .id_table = mc5587_gpio_id,
  681. };
  682. static int __init mc5587_gpio_init(void)
  683. {
  684. return i2c_add_driver(&mc5587_gpio_driver);
  685. }
  686. module_init(mc5587_gpio_init);
  687. static void __exit mc5587_gpio_exit(void)
  688. {
  689. i2c_del_driver(&mc5587_gpio_driver);
  690. }
  691. module_exit(mc5587_gpio_exit);
  692. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  693. MODULE_DESCRIPTION("GPIO MC5587 Driver");
  694. MODULE_LICENSE("GPL");