gpio-sx150x.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. */
  17. #include <linux/gpio.h>
  18. #include <linux/i2c.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c/sx150x.h>
  26. #include <linux/of.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/of_device.h>
  31. #define NO_UPDATE_PENDING -1
  32. /* The chip models of sx150x */
  33. #define SX150X_123 0
  34. #define SX150X_456 1
  35. #define SX150X_789 2
  36. struct sx150x_123_pri {
  37. u8 reg_pld_mode;
  38. u8 reg_pld_table0;
  39. u8 reg_pld_table1;
  40. u8 reg_pld_table2;
  41. u8 reg_pld_table3;
  42. u8 reg_pld_table4;
  43. u8 reg_advance;
  44. };
  45. struct sx150x_456_pri {
  46. u8 reg_pld_mode;
  47. u8 reg_pld_table0;
  48. u8 reg_pld_table1;
  49. u8 reg_pld_table2;
  50. u8 reg_pld_table3;
  51. u8 reg_pld_table4;
  52. u8 reg_advance;
  53. };
  54. struct sx150x_789_pri {
  55. u8 reg_drain;
  56. u8 reg_polarity;
  57. u8 reg_clock;
  58. u8 reg_misc;
  59. u8 reg_reset;
  60. u8 ngpios;
  61. };
  62. struct sx150x_device_data {
  63. u8 model;
  64. u8 reg_pullup;
  65. u8 reg_pulldn;
  66. u8 reg_dir;
  67. u8 reg_data;
  68. u8 reg_irq_mask;
  69. u8 reg_irq_src;
  70. u8 reg_sense;
  71. u8 ngpios;
  72. union {
  73. struct sx150x_123_pri x123;
  74. struct sx150x_456_pri x456;
  75. struct sx150x_789_pri x789;
  76. } pri;
  77. };
  78. struct sx150x_chip {
  79. struct gpio_chip gpio_chip;
  80. struct i2c_client *client;
  81. const struct sx150x_device_data *dev_cfg;
  82. int irq_summary;
  83. int irq_base;
  84. int irq_update;
  85. u32 irq_sense;
  86. u32 irq_masked;
  87. u32 dev_sense;
  88. u32 dev_masked;
  89. struct irq_chip irq_chip;
  90. struct mutex lock;
  91. };
  92. static const struct sx150x_device_data sx150x_devices[] = {
  93. [0] = { /* sx1508q */
  94. .model = SX150X_789,
  95. .reg_pullup = 0x03,
  96. .reg_pulldn = 0x04,
  97. .reg_dir = 0x07,
  98. .reg_data = 0x08,
  99. .reg_irq_mask = 0x09,
  100. .reg_irq_src = 0x0c,
  101. .reg_sense = 0x0b,
  102. .pri.x789 = {
  103. .reg_drain = 0x05,
  104. .reg_polarity = 0x06,
  105. .reg_clock = 0x0f,
  106. .reg_misc = 0x10,
  107. .reg_reset = 0x7d,
  108. },
  109. .ngpios = 8,
  110. },
  111. [1] = { /* sx1509q */
  112. .model = SX150X_789,
  113. .reg_pullup = 0x07,
  114. .reg_pulldn = 0x09,
  115. .reg_dir = 0x0f,
  116. .reg_data = 0x11,
  117. .reg_irq_mask = 0x13,
  118. .reg_irq_src = 0x19,
  119. .reg_sense = 0x17,
  120. .pri.x789 = {
  121. .reg_drain = 0x0b,
  122. .reg_polarity = 0x0d,
  123. .reg_clock = 0x1e,
  124. .reg_misc = 0x1f,
  125. .reg_reset = 0x7d,
  126. },
  127. .ngpios = 16
  128. },
  129. [2] = { /* sx1506q */
  130. .model = SX150X_456,
  131. .reg_pullup = 0x05,
  132. .reg_pulldn = 0x07,
  133. .reg_dir = 0x03,
  134. .reg_data = 0x01,
  135. .reg_irq_mask = 0x09,
  136. .reg_irq_src = 0x0f,
  137. .reg_sense = 0x0d,
  138. .pri.x456 = {
  139. .reg_pld_mode = 0x21,
  140. .reg_pld_table0 = 0x23,
  141. .reg_pld_table1 = 0x25,
  142. .reg_pld_table2 = 0x27,
  143. .reg_pld_table3 = 0x29,
  144. .reg_pld_table4 = 0x2b,
  145. .reg_advance = 0xad,
  146. },
  147. .ngpios = 16
  148. },
  149. [3] = { /* sx1502q */
  150. .model = SX150X_123,
  151. .reg_pullup = 0x02,
  152. .reg_pulldn = 0x03,
  153. .reg_dir = 0x01,
  154. .reg_data = 0x00,
  155. .reg_irq_mask = 0x05,
  156. .reg_irq_src = 0x08,
  157. .reg_sense = 0x07,
  158. .pri.x123 = {
  159. .reg_pld_mode = 0x10,
  160. .reg_pld_table0 = 0x11,
  161. .reg_pld_table1 = 0x12,
  162. .reg_pld_table2 = 0x13,
  163. .reg_pld_table3 = 0x14,
  164. .reg_pld_table4 = 0x15,
  165. .reg_advance = 0xad,
  166. },
  167. .ngpios = 8,
  168. },
  169. };
  170. static const struct i2c_device_id sx150x_id[] = {
  171. {"sx1508q", 0},
  172. {"sx1509q", 1},
  173. {"sx1506q", 2},
  174. {"sx1502q", 3},
  175. {}
  176. };
  177. MODULE_DEVICE_TABLE(i2c, sx150x_id);
  178. static const struct of_device_id sx150x_of_match[] = {
  179. { .compatible = "semtech,sx1508q" },
  180. { .compatible = "semtech,sx1509q" },
  181. { .compatible = "semtech,sx1506q" },
  182. { .compatible = "semtech,sx1502q" },
  183. {},
  184. };
  185. MODULE_DEVICE_TABLE(of, sx150x_of_match);
  186. static s32 sx150x_i2c_write(struct i2c_client *client, u8 reg, u8 val)
  187. {
  188. s32 err = i2c_smbus_write_byte_data(client, reg, val);
  189. if (err < 0)
  190. dev_warn(&client->dev,
  191. "i2c write fail: can't write %02x to %02x: %d\n",
  192. val, reg, err);
  193. return err;
  194. }
  195. static s32 sx150x_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
  196. {
  197. s32 err = i2c_smbus_read_byte_data(client, reg);
  198. if (err >= 0)
  199. *val = err;
  200. else
  201. dev_warn(&client->dev,
  202. "i2c read fail: can't read from %02x: %d\n",
  203. reg, err);
  204. return err;
  205. }
  206. static inline bool offset_is_oscio(struct sx150x_chip *chip, unsigned offset)
  207. {
  208. return (chip->dev_cfg->ngpios == offset);
  209. }
  210. /*
  211. * These utility functions solve the common problem of locating and setting
  212. * configuration bits. Configuration bits are grouped into registers
  213. * whose indexes increase downwards. For example, with eight-bit registers,
  214. * sixteen gpios would have their config bits grouped in the following order:
  215. * REGISTER N-1 [ f e d c b a 9 8 ]
  216. * N [ 7 6 5 4 3 2 1 0 ]
  217. *
  218. * For multi-bit configurations, the pattern gets wider:
  219. * REGISTER N-3 [ f f e e d d c c ]
  220. * N-2 [ b b a a 9 9 8 8 ]
  221. * N-1 [ 7 7 6 6 5 5 4 4 ]
  222. * N [ 3 3 2 2 1 1 0 0 ]
  223. *
  224. * Given the address of the starting register 'N', the index of the gpio
  225. * whose configuration we seek to change, and the width in bits of that
  226. * configuration, these functions allow us to locate the correct
  227. * register and mask the correct bits.
  228. */
  229. static inline void sx150x_find_cfg(u8 offset, u8 width,
  230. u8 *reg, u8 *mask, u8 *shift)
  231. {
  232. *reg -= offset * width / 8;
  233. *mask = (1 << width) - 1;
  234. *shift = (offset * width) % 8;
  235. *mask <<= *shift;
  236. }
  237. static s32 sx150x_write_cfg(struct sx150x_chip *chip,
  238. u8 offset, u8 width, u8 reg, u8 val)
  239. {
  240. u8 mask;
  241. u8 data;
  242. u8 shift;
  243. s32 err;
  244. sx150x_find_cfg(offset, width, &reg, &mask, &shift);
  245. err = sx150x_i2c_read(chip->client, reg, &data);
  246. if (err < 0)
  247. return err;
  248. data &= ~mask;
  249. data |= (val << shift) & mask;
  250. return sx150x_i2c_write(chip->client, reg, data);
  251. }
  252. static int sx150x_get_io(struct sx150x_chip *chip, unsigned offset)
  253. {
  254. u8 reg = chip->dev_cfg->reg_data;
  255. u8 mask;
  256. u8 data;
  257. u8 shift;
  258. s32 err;
  259. sx150x_find_cfg(offset, 1, &reg, &mask, &shift);
  260. err = sx150x_i2c_read(chip->client, reg, &data);
  261. if (err >= 0)
  262. err = (data & mask) != 0 ? 1 : 0;
  263. return err;
  264. }
  265. static void sx150x_set_oscio(struct sx150x_chip *chip, int val)
  266. {
  267. sx150x_i2c_write(chip->client,
  268. chip->dev_cfg->pri.x789.reg_clock,
  269. (val ? 0x1f : 0x10));
  270. }
  271. static void sx150x_set_io(struct sx150x_chip *chip, unsigned offset, int val)
  272. {
  273. sx150x_write_cfg(chip,
  274. offset,
  275. 1,
  276. chip->dev_cfg->reg_data,
  277. (val ? 1 : 0));
  278. }
  279. static int sx150x_io_input(struct sx150x_chip *chip, unsigned offset)
  280. {
  281. return sx150x_write_cfg(chip,
  282. offset,
  283. 1,
  284. chip->dev_cfg->reg_dir,
  285. 1);
  286. }
  287. static int sx150x_io_output(struct sx150x_chip *chip, unsigned offset, int val)
  288. {
  289. int err;
  290. err = sx150x_write_cfg(chip,
  291. offset,
  292. 1,
  293. chip->dev_cfg->reg_data,
  294. (val ? 1 : 0));
  295. if (err >= 0)
  296. err = sx150x_write_cfg(chip,
  297. offset,
  298. 1,
  299. chip->dev_cfg->reg_dir,
  300. 0);
  301. return err;
  302. }
  303. static int sx150x_gpio_get(struct gpio_chip *gc, unsigned offset)
  304. {
  305. struct sx150x_chip *chip = gpiochip_get_data(gc);
  306. int status = -EINVAL;
  307. if (!offset_is_oscio(chip, offset)) {
  308. mutex_lock(&chip->lock);
  309. status = sx150x_get_io(chip, offset);
  310. mutex_unlock(&chip->lock);
  311. }
  312. return (status < 0) ? status : !!status;
  313. }
  314. static void sx150x_gpio_set(struct gpio_chip *gc, unsigned offset, int val)
  315. {
  316. struct sx150x_chip *chip = gpiochip_get_data(gc);
  317. mutex_lock(&chip->lock);
  318. if (offset_is_oscio(chip, offset))
  319. sx150x_set_oscio(chip, val);
  320. else
  321. sx150x_set_io(chip, offset, val);
  322. mutex_unlock(&chip->lock);
  323. }
  324. static int sx150x_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  325. {
  326. struct sx150x_chip *chip = gpiochip_get_data(gc);
  327. int status = -EINVAL;
  328. if (!offset_is_oscio(chip, offset)) {
  329. mutex_lock(&chip->lock);
  330. status = sx150x_io_input(chip, offset);
  331. mutex_unlock(&chip->lock);
  332. }
  333. return status;
  334. }
  335. static int sx150x_gpio_direction_output(struct gpio_chip *gc,
  336. unsigned offset,
  337. int val)
  338. {
  339. struct sx150x_chip *chip = gpiochip_get_data(gc);
  340. int status = 0;
  341. if (!offset_is_oscio(chip, offset)) {
  342. mutex_lock(&chip->lock);
  343. status = sx150x_io_output(chip, offset, val);
  344. mutex_unlock(&chip->lock);
  345. }
  346. return status;
  347. }
  348. static void sx150x_irq_mask(struct irq_data *d)
  349. {
  350. struct sx150x_chip *chip = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  351. unsigned n = d->hwirq;
  352. chip->irq_masked |= (1 << n);
  353. chip->irq_update = n;
  354. }
  355. static void sx150x_irq_unmask(struct irq_data *d)
  356. {
  357. struct sx150x_chip *chip = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  358. unsigned n = d->hwirq;
  359. chip->irq_masked &= ~(1 << n);
  360. chip->irq_update = n;
  361. }
  362. static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  363. {
  364. struct sx150x_chip *chip = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  365. unsigned n, val = 0;
  366. if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  367. return -EINVAL;
  368. n = d->hwirq;
  369. if (flow_type & IRQ_TYPE_EDGE_RISING)
  370. val |= 0x1;
  371. if (flow_type & IRQ_TYPE_EDGE_FALLING)
  372. val |= 0x2;
  373. chip->irq_sense &= ~(3UL << (n * 2));
  374. chip->irq_sense |= val << (n * 2);
  375. chip->irq_update = n;
  376. return 0;
  377. }
  378. static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
  379. {
  380. struct sx150x_chip *chip = (struct sx150x_chip *)dev_id;
  381. unsigned nhandled = 0;
  382. unsigned sub_irq;
  383. unsigned n;
  384. s32 err;
  385. u8 val;
  386. int i;
  387. for (i = (chip->dev_cfg->ngpios / 8) - 1; i >= 0; --i) {
  388. err = sx150x_i2c_read(chip->client,
  389. chip->dev_cfg->reg_irq_src - i,
  390. &val);
  391. if (err < 0)
  392. continue;
  393. sx150x_i2c_write(chip->client,
  394. chip->dev_cfg->reg_irq_src - i,
  395. val);
  396. for (n = 0; n < 8; ++n) {
  397. if (val & (1 << n)) {
  398. sub_irq = irq_find_mapping(
  399. chip->gpio_chip.irqdomain,
  400. (i * 8) + n);
  401. handle_nested_irq(sub_irq);
  402. ++nhandled;
  403. }
  404. }
  405. }
  406. return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
  407. }
  408. static void sx150x_irq_bus_lock(struct irq_data *d)
  409. {
  410. struct sx150x_chip *chip = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  411. mutex_lock(&chip->lock);
  412. }
  413. static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
  414. {
  415. struct sx150x_chip *chip = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  416. unsigned n;
  417. if (chip->irq_update == NO_UPDATE_PENDING)
  418. goto out;
  419. n = chip->irq_update;
  420. chip->irq_update = NO_UPDATE_PENDING;
  421. /* Avoid updates if nothing changed */
  422. if (chip->dev_sense == chip->irq_sense &&
  423. chip->dev_masked == chip->irq_masked)
  424. goto out;
  425. chip->dev_sense = chip->irq_sense;
  426. chip->dev_masked = chip->irq_masked;
  427. if (chip->irq_masked & (1 << n)) {
  428. sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 1);
  429. sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense, 0);
  430. } else {
  431. sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 0);
  432. sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense,
  433. chip->irq_sense >> (n * 2));
  434. }
  435. out:
  436. mutex_unlock(&chip->lock);
  437. }
  438. static void sx150x_init_chip(struct sx150x_chip *chip,
  439. struct i2c_client *client,
  440. kernel_ulong_t driver_data,
  441. struct sx150x_platform_data *pdata)
  442. {
  443. mutex_init(&chip->lock);
  444. chip->client = client;
  445. chip->dev_cfg = &sx150x_devices[driver_data];
  446. chip->gpio_chip.parent = &client->dev;
  447. chip->gpio_chip.label = client->name;
  448. chip->gpio_chip.direction_input = sx150x_gpio_direction_input;
  449. chip->gpio_chip.direction_output = sx150x_gpio_direction_output;
  450. chip->gpio_chip.get = sx150x_gpio_get;
  451. chip->gpio_chip.set = sx150x_gpio_set;
  452. chip->gpio_chip.base = pdata->gpio_base;
  453. chip->gpio_chip.can_sleep = true;
  454. chip->gpio_chip.ngpio = chip->dev_cfg->ngpios;
  455. #ifdef CONFIG_OF_GPIO
  456. chip->gpio_chip.of_node = client->dev.of_node;
  457. chip->gpio_chip.of_gpio_n_cells = 2;
  458. #endif
  459. if (pdata->oscio_is_gpo)
  460. ++chip->gpio_chip.ngpio;
  461. chip->irq_chip.name = client->name;
  462. chip->irq_chip.irq_mask = sx150x_irq_mask;
  463. chip->irq_chip.irq_unmask = sx150x_irq_unmask;
  464. chip->irq_chip.irq_set_type = sx150x_irq_set_type;
  465. chip->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
  466. chip->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
  467. chip->irq_summary = -1;
  468. chip->irq_base = -1;
  469. chip->irq_masked = ~0;
  470. chip->irq_sense = 0;
  471. chip->dev_masked = ~0;
  472. chip->dev_sense = 0;
  473. chip->irq_update = NO_UPDATE_PENDING;
  474. }
  475. static int sx150x_init_io(struct sx150x_chip *chip, u8 base, u16 cfg)
  476. {
  477. int err = 0;
  478. unsigned n;
  479. for (n = 0; err >= 0 && n < (chip->dev_cfg->ngpios / 8); ++n)
  480. err = sx150x_i2c_write(chip->client, base - n, cfg >> (n * 8));
  481. return err;
  482. }
  483. static int sx150x_reset(struct sx150x_chip *chip)
  484. {
  485. int err;
  486. err = i2c_smbus_write_byte_data(chip->client,
  487. chip->dev_cfg->pri.x789.reg_reset,
  488. 0x12);
  489. if (err < 0)
  490. return err;
  491. err = i2c_smbus_write_byte_data(chip->client,
  492. chip->dev_cfg->pri.x789.reg_reset,
  493. 0x34);
  494. return err;
  495. }
  496. static int sx150x_init_hw(struct sx150x_chip *chip,
  497. struct sx150x_platform_data *pdata)
  498. {
  499. int err = 0;
  500. if (pdata->reset_during_probe) {
  501. err = sx150x_reset(chip);
  502. if (err < 0)
  503. return err;
  504. }
  505. if (chip->dev_cfg->model == SX150X_789)
  506. err = sx150x_i2c_write(chip->client,
  507. chip->dev_cfg->pri.x789.reg_misc,
  508. 0x01);
  509. else if (chip->dev_cfg->model == SX150X_456)
  510. err = sx150x_i2c_write(chip->client,
  511. chip->dev_cfg->pri.x456.reg_advance,
  512. 0x04);
  513. else
  514. err = sx150x_i2c_write(chip->client,
  515. chip->dev_cfg->pri.x123.reg_advance,
  516. 0x00);
  517. if (err < 0)
  518. return err;
  519. err = sx150x_init_io(chip, chip->dev_cfg->reg_pullup,
  520. pdata->io_pullup_ena);
  521. if (err < 0)
  522. return err;
  523. err = sx150x_init_io(chip, chip->dev_cfg->reg_pulldn,
  524. pdata->io_pulldn_ena);
  525. if (err < 0)
  526. return err;
  527. if (chip->dev_cfg->model == SX150X_789) {
  528. err = sx150x_init_io(chip,
  529. chip->dev_cfg->pri.x789.reg_drain,
  530. pdata->io_open_drain_ena);
  531. if (err < 0)
  532. return err;
  533. err = sx150x_init_io(chip,
  534. chip->dev_cfg->pri.x789.reg_polarity,
  535. pdata->io_polarity);
  536. if (err < 0)
  537. return err;
  538. } else if (chip->dev_cfg->model == SX150X_456) {
  539. /* Set all pins to work in normal mode */
  540. err = sx150x_init_io(chip,
  541. chip->dev_cfg->pri.x456.reg_pld_mode,
  542. 0);
  543. if (err < 0)
  544. return err;
  545. } else {
  546. /* Set all pins to work in normal mode */
  547. err = sx150x_init_io(chip,
  548. chip->dev_cfg->pri.x123.reg_pld_mode,
  549. 0);
  550. if (err < 0)
  551. return err;
  552. }
  553. if (pdata->oscio_is_gpo)
  554. sx150x_set_oscio(chip, 0);
  555. return err;
  556. }
  557. static int sx150x_install_irq_chip(struct sx150x_chip *chip,
  558. int irq_summary,
  559. int irq_base)
  560. {
  561. int err;
  562. chip->irq_summary = irq_summary;
  563. chip->irq_base = irq_base;
  564. /* Add gpio chip to irq subsystem */
  565. err = gpiochip_irqchip_add(&chip->gpio_chip,
  566. &chip->irq_chip, chip->irq_base,
  567. handle_edge_irq, IRQ_TYPE_EDGE_BOTH);
  568. if (err) {
  569. dev_err(&chip->client->dev,
  570. "could not connect irqchip to gpiochip\n");
  571. return err;
  572. }
  573. err = devm_request_threaded_irq(&chip->client->dev,
  574. irq_summary, NULL, sx150x_irq_thread_fn,
  575. IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_FALLING,
  576. chip->irq_chip.name, chip);
  577. if (err < 0) {
  578. chip->irq_summary = -1;
  579. chip->irq_base = -1;
  580. }
  581. return err;
  582. }
  583. static int sx150x_probe(struct i2c_client *client,
  584. const struct i2c_device_id *id)
  585. {
  586. static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
  587. I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  588. struct sx150x_platform_data *pdata;
  589. struct sx150x_chip *chip;
  590. int rc;
  591. pdata = dev_get_platdata(&client->dev);
  592. if (!pdata)
  593. return -EINVAL;
  594. if (!i2c_check_functionality(client->adapter, i2c_funcs))
  595. return -ENOSYS;
  596. chip = devm_kzalloc(&client->dev,
  597. sizeof(struct sx150x_chip), GFP_KERNEL);
  598. if (!chip)
  599. return -ENOMEM;
  600. sx150x_init_chip(chip, client, id->driver_data, pdata);
  601. rc = sx150x_init_hw(chip, pdata);
  602. if (rc < 0)
  603. return rc;
  604. rc = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
  605. if (rc)
  606. return rc;
  607. if (pdata->irq_summary >= 0) {
  608. rc = sx150x_install_irq_chip(chip,
  609. pdata->irq_summary,
  610. pdata->irq_base);
  611. if (rc < 0)
  612. return rc;
  613. }
  614. i2c_set_clientdata(client, chip);
  615. return 0;
  616. }
  617. static struct i2c_driver sx150x_driver = {
  618. .driver = {
  619. .name = "sx150x",
  620. .of_match_table = of_match_ptr(sx150x_of_match),
  621. },
  622. .probe = sx150x_probe,
  623. .id_table = sx150x_id,
  624. };
  625. static int __init sx150x_init(void)
  626. {
  627. return i2c_add_driver(&sx150x_driver);
  628. }
  629. subsys_initcall(sx150x_init);
  630. static void __exit sx150x_exit(void)
  631. {
  632. return i2c_del_driver(&sx150x_driver);
  633. }
  634. module_exit(sx150x_exit);
  635. MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
  636. MODULE_DESCRIPTION("Driver for Semtech SX150X I2C GPIO Expanders");
  637. MODULE_LICENSE("GPL v2");