gpio-sx150x.c 20 KB

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