qcom-i2c-pmic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /* Copyright (c) 2016-2017 The Linux Foundation. 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. #define pr_fmt(fmt) "I2C PMIC: %s: " fmt, __func__
  13. #include <linux/bitops.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/module.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/pinctrl/consumer.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #define I2C_INTR_STATUS_BASE 0x0550
  24. #define INT_RT_STS_OFFSET 0x10
  25. #define INT_SET_TYPE_OFFSET 0x11
  26. #define INT_POL_HIGH_OFFSET 0x12
  27. #define INT_POL_LOW_OFFSET 0x13
  28. #define INT_LATCHED_CLR_OFFSET 0x14
  29. #define INT_EN_SET_OFFSET 0x15
  30. #define INT_EN_CLR_OFFSET 0x16
  31. #define INT_LATCHED_STS_OFFSET 0x18
  32. #define INT_PENDING_STS_OFFSET 0x19
  33. #define INT_MID_SEL_OFFSET 0x1A
  34. #define INT_MID_SEL_MASK GENMASK(1, 0)
  35. #define INT_PRIORITY_OFFSET 0x1B
  36. #define INT_PRIORITY_BIT BIT(0)
  37. enum {
  38. IRQ_SET_TYPE = 0,
  39. IRQ_POL_HIGH,
  40. IRQ_POL_LOW,
  41. IRQ_LATCHED_CLR, /* not needed but makes life easy */
  42. IRQ_EN_SET,
  43. IRQ_MAX_REGS,
  44. };
  45. struct i2c_pmic_periph {
  46. void *data;
  47. u16 addr;
  48. u8 cached[IRQ_MAX_REGS];
  49. u8 synced[IRQ_MAX_REGS];
  50. u8 wake;
  51. struct mutex lock;
  52. };
  53. struct i2c_pmic {
  54. struct device *dev;
  55. struct regmap *regmap;
  56. struct irq_domain *domain;
  57. struct i2c_pmic_periph *periph;
  58. struct pinctrl *pinctrl;
  59. struct mutex irq_complete;
  60. const char *pinctrl_name;
  61. int num_periphs;
  62. int summary_irq;
  63. bool resume_completed;
  64. bool irq_waiting;
  65. };
  66. static void i2c_pmic_irq_bus_lock(struct irq_data *d)
  67. {
  68. struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);
  69. mutex_lock(&periph->lock);
  70. }
  71. static void i2c_pmic_sync_type_polarity(struct i2c_pmic *chip,
  72. struct i2c_pmic_periph *periph)
  73. {
  74. int rc;
  75. /* did any irq type change? */
  76. if (periph->cached[IRQ_SET_TYPE] ^ periph->synced[IRQ_SET_TYPE]) {
  77. rc = regmap_write(chip->regmap,
  78. periph->addr | INT_SET_TYPE_OFFSET,
  79. periph->cached[IRQ_SET_TYPE]);
  80. if (rc < 0) {
  81. pr_err("Couldn't set periph 0x%04x irqs 0x%02x type rc=%d\n",
  82. periph->addr, periph->cached[IRQ_SET_TYPE], rc);
  83. return;
  84. }
  85. periph->synced[IRQ_SET_TYPE] = periph->cached[IRQ_SET_TYPE];
  86. }
  87. /* did any polarity high change? */
  88. if (periph->cached[IRQ_POL_HIGH] ^ periph->synced[IRQ_POL_HIGH]) {
  89. rc = regmap_write(chip->regmap,
  90. periph->addr | INT_POL_HIGH_OFFSET,
  91. periph->cached[IRQ_POL_HIGH]);
  92. if (rc < 0) {
  93. pr_err("Couldn't set periph 0x%04x irqs 0x%02x polarity high rc=%d\n",
  94. periph->addr, periph->cached[IRQ_POL_HIGH], rc);
  95. return;
  96. }
  97. periph->synced[IRQ_POL_HIGH] = periph->cached[IRQ_POL_HIGH];
  98. }
  99. /* did any polarity low change? */
  100. if (periph->cached[IRQ_POL_LOW] ^ periph->synced[IRQ_POL_LOW]) {
  101. rc = regmap_write(chip->regmap,
  102. periph->addr | INT_POL_LOW_OFFSET,
  103. periph->cached[IRQ_POL_LOW]);
  104. if (rc < 0) {
  105. pr_err("Couldn't set periph 0x%04x irqs 0x%02x polarity low rc=%d\n",
  106. periph->addr, periph->cached[IRQ_POL_LOW], rc);
  107. return;
  108. }
  109. periph->synced[IRQ_POL_LOW] = periph->cached[IRQ_POL_LOW];
  110. }
  111. }
  112. static void i2c_pmic_sync_enable(struct i2c_pmic *chip,
  113. struct i2c_pmic_periph *periph)
  114. {
  115. u8 en_set, en_clr;
  116. int rc;
  117. /* determine which irqs were enabled and which were disabled */
  118. en_clr = periph->synced[IRQ_EN_SET] & ~periph->cached[IRQ_EN_SET];
  119. en_set = ~periph->synced[IRQ_EN_SET] & periph->cached[IRQ_EN_SET];
  120. /* were any irqs disabled? */
  121. if (en_clr) {
  122. rc = regmap_write(chip->regmap,
  123. periph->addr | INT_EN_CLR_OFFSET, en_clr);
  124. if (rc < 0) {
  125. pr_err("Couldn't disable periph 0x%04x irqs 0x%02x rc=%d\n",
  126. periph->addr, en_clr, rc);
  127. return;
  128. }
  129. }
  130. /* were any irqs enabled? */
  131. if (en_set) {
  132. rc = regmap_write(chip->regmap,
  133. periph->addr | INT_EN_SET_OFFSET, en_set);
  134. if (rc < 0) {
  135. pr_err("Couldn't enable periph 0x%04x irqs 0x%02x rc=%d\n",
  136. periph->addr, en_set, rc);
  137. return;
  138. }
  139. }
  140. /* irq enabled status was written to hardware */
  141. periph->synced[IRQ_EN_SET] = periph->cached[IRQ_EN_SET];
  142. }
  143. static void i2c_pmic_irq_bus_sync_unlock(struct irq_data *d)
  144. {
  145. struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);
  146. struct i2c_pmic *chip = periph->data;
  147. i2c_pmic_sync_type_polarity(chip, periph);
  148. i2c_pmic_sync_enable(chip, periph);
  149. mutex_unlock(&periph->lock);
  150. }
  151. static void i2c_pmic_irq_disable(struct irq_data *d)
  152. {
  153. struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);
  154. periph->cached[IRQ_EN_SET] &= ~d->hwirq & 0xFF;
  155. }
  156. static void i2c_pmic_irq_enable(struct irq_data *d)
  157. {
  158. struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);
  159. periph->cached[IRQ_EN_SET] |= d->hwirq & 0xFF;
  160. }
  161. static int i2c_pmic_irq_set_type(struct irq_data *d, unsigned int irq_type)
  162. {
  163. struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);
  164. switch (irq_type) {
  165. case IRQ_TYPE_EDGE_RISING:
  166. periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF;
  167. periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF;
  168. periph->cached[IRQ_POL_LOW] &= ~d->hwirq & 0xFF;
  169. break;
  170. case IRQ_TYPE_EDGE_FALLING:
  171. periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF;
  172. periph->cached[IRQ_POL_HIGH] &= ~d->hwirq & 0xFF;
  173. periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF;
  174. break;
  175. case IRQ_TYPE_EDGE_BOTH:
  176. periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF;
  177. periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF;
  178. periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF;
  179. break;
  180. case IRQ_TYPE_LEVEL_HIGH:
  181. periph->cached[IRQ_SET_TYPE] &= ~d->hwirq & 0xFF;
  182. periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF;
  183. periph->cached[IRQ_POL_LOW] &= ~d->hwirq & 0xFF;
  184. break;
  185. case IRQ_TYPE_LEVEL_LOW:
  186. periph->cached[IRQ_SET_TYPE] &= ~d->hwirq & 0xFF;
  187. periph->cached[IRQ_POL_HIGH] &= ~d->hwirq & 0xFF;
  188. periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF;
  189. break;
  190. default:
  191. pr_err("irq type 0x%04x is not supported\n", irq_type);
  192. return -EINVAL;
  193. }
  194. return 0;
  195. }
  196. #ifdef CONFIG_PM_SLEEP
  197. static int i2c_pmic_irq_set_wake(struct irq_data *d, unsigned int on)
  198. {
  199. struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);
  200. if (on)
  201. periph->wake |= d->hwirq & 0xFF;
  202. else
  203. periph->wake &= ~d->hwirq & 0xFF;
  204. return 0;
  205. }
  206. #else
  207. #define i2c_pmic_irq_set_wake NULL
  208. #endif
  209. static struct irq_chip i2c_pmic_irq_chip = {
  210. .name = "i2c_pmic_irq_chip",
  211. .irq_bus_lock = i2c_pmic_irq_bus_lock,
  212. .irq_bus_sync_unlock = i2c_pmic_irq_bus_sync_unlock,
  213. .irq_disable = i2c_pmic_irq_disable,
  214. .irq_enable = i2c_pmic_irq_enable,
  215. .irq_set_type = i2c_pmic_irq_set_type,
  216. .irq_set_wake = i2c_pmic_irq_set_wake,
  217. };
  218. static struct i2c_pmic_periph *i2c_pmic_find_periph(struct i2c_pmic *chip,
  219. irq_hw_number_t hwirq)
  220. {
  221. int i;
  222. for (i = 0; i < chip->num_periphs; i++)
  223. if (chip->periph[i].addr == (hwirq & 0xFF00))
  224. return &chip->periph[i];
  225. pr_err_ratelimited("Couldn't find periph struct for hwirq 0x%04lx\n",
  226. hwirq);
  227. return NULL;
  228. }
  229. static int i2c_pmic_domain_map(struct irq_domain *d, unsigned int virq,
  230. irq_hw_number_t hwirq)
  231. {
  232. struct i2c_pmic *chip = d->host_data;
  233. struct i2c_pmic_periph *periph = i2c_pmic_find_periph(chip, hwirq);
  234. if (!periph)
  235. return -ENODEV;
  236. irq_set_chip_data(virq, periph);
  237. irq_set_chip_and_handler(virq, &i2c_pmic_irq_chip, handle_level_irq);
  238. irq_set_nested_thread(virq, 1);
  239. irq_set_noprobe(virq);
  240. return 0;
  241. }
  242. static int i2c_pmic_domain_xlate(struct irq_domain *d,
  243. struct device_node *ctrlr, const u32 *intspec,
  244. unsigned int intsize, unsigned long *out_hwirq,
  245. unsigned int *out_type)
  246. {
  247. if (intsize != 3)
  248. return -EINVAL;
  249. if (intspec[0] > 0xFF || intspec[1] > 0x7 ||
  250. intspec[2] > IRQ_TYPE_SENSE_MASK)
  251. return -EINVAL;
  252. /*
  253. * Interrupt specifiers are triplets
  254. * <peripheral-address, irq-number, IRQ_TYPE_*>
  255. *
  256. * peripheral-address - The base address of the peripheral
  257. * irq-number - The zero based bit position of the peripheral's
  258. * interrupt registers corresponding to the irq
  259. * where the LSB is 0 and the MSB is 7
  260. * IRQ_TYPE_* - Please refer to linux/irq.h
  261. */
  262. *out_hwirq = intspec[0] << 8 | BIT(intspec[1]);
  263. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  264. return 0;
  265. }
  266. static const struct irq_domain_ops i2c_pmic_domain_ops = {
  267. .map = i2c_pmic_domain_map,
  268. .xlate = i2c_pmic_domain_xlate,
  269. };
  270. static void i2c_pmic_irq_ack_now(struct i2c_pmic *chip, u16 hwirq)
  271. {
  272. int rc;
  273. rc = regmap_write(chip->regmap,
  274. (hwirq & 0xFF00) | INT_LATCHED_CLR_OFFSET,
  275. hwirq & 0xFF);
  276. if (rc < 0)
  277. pr_err_ratelimited("Couldn't ack 0x%04x rc=%d\n", hwirq, rc);
  278. }
  279. static void i2c_pmic_irq_disable_now(struct i2c_pmic *chip, u16 hwirq)
  280. {
  281. struct i2c_pmic_periph *periph = i2c_pmic_find_periph(chip, hwirq);
  282. int rc;
  283. if (!periph)
  284. return;
  285. mutex_lock(&periph->lock);
  286. periph->cached[IRQ_EN_SET] &= ~hwirq & 0xFF;
  287. rc = regmap_write(chip->regmap,
  288. (hwirq & 0xFF00) | INT_EN_CLR_OFFSET,
  289. hwirq & 0xFF);
  290. if (rc < 0) {
  291. pr_err_ratelimited("Couldn't disable irq 0x%04x rc=%d\n",
  292. hwirq, rc);
  293. goto unlock;
  294. }
  295. periph->synced[IRQ_EN_SET] = periph->cached[IRQ_EN_SET];
  296. unlock:
  297. mutex_unlock(&periph->lock);
  298. }
  299. static void i2c_pmic_periph_status_handler(struct i2c_pmic *chip,
  300. u16 periph_address, u8 periph_status)
  301. {
  302. unsigned int hwirq, virq;
  303. int i;
  304. while (periph_status) {
  305. i = ffs(periph_status) - 1;
  306. periph_status &= ~BIT(i);
  307. hwirq = periph_address | BIT(i);
  308. virq = irq_find_mapping(chip->domain, hwirq);
  309. if (virq == 0) {
  310. pr_err_ratelimited("Couldn't find mapping; disabling 0x%04x\n",
  311. hwirq);
  312. i2c_pmic_irq_disable_now(chip, hwirq);
  313. continue;
  314. }
  315. handle_nested_irq(virq);
  316. i2c_pmic_irq_ack_now(chip, hwirq);
  317. }
  318. }
  319. static void i2c_pmic_summary_status_handler(struct i2c_pmic *chip,
  320. struct i2c_pmic_periph *periph,
  321. u8 summary_status)
  322. {
  323. unsigned int periph_status;
  324. int rc, i;
  325. while (summary_status) {
  326. i = ffs(summary_status) - 1;
  327. summary_status &= ~BIT(i);
  328. rc = regmap_read(chip->regmap,
  329. periph[i].addr | INT_LATCHED_STS_OFFSET,
  330. &periph_status);
  331. if (rc < 0) {
  332. pr_err_ratelimited("Couldn't read 0x%04x | INT_LATCHED_STS rc=%d\n",
  333. periph[i].addr, rc);
  334. continue;
  335. }
  336. i2c_pmic_periph_status_handler(chip, periph[i].addr,
  337. periph_status);
  338. }
  339. }
  340. static irqreturn_t i2c_pmic_irq_handler(int irq, void *dev_id)
  341. {
  342. struct i2c_pmic *chip = dev_id;
  343. struct i2c_pmic_periph *periph;
  344. unsigned int summary_status;
  345. int rc, i;
  346. mutex_lock(&chip->irq_complete);
  347. chip->irq_waiting = true;
  348. if (!chip->resume_completed) {
  349. pr_debug("IRQ triggered before device-resume\n");
  350. disable_irq_nosync(irq);
  351. mutex_unlock(&chip->irq_complete);
  352. return IRQ_HANDLED;
  353. }
  354. chip->irq_waiting = false;
  355. for (i = 0; i < DIV_ROUND_UP(chip->num_periphs, BITS_PER_BYTE); i++) {
  356. rc = regmap_read(chip->regmap, I2C_INTR_STATUS_BASE + i,
  357. &summary_status);
  358. if (rc < 0) {
  359. pr_err_ratelimited("Couldn't read I2C_INTR_STATUS%d rc=%d\n",
  360. i, rc);
  361. continue;
  362. }
  363. if (summary_status == 0)
  364. continue;
  365. periph = &chip->periph[i * 8];
  366. i2c_pmic_summary_status_handler(chip, periph, summary_status);
  367. }
  368. mutex_unlock(&chip->irq_complete);
  369. return IRQ_HANDLED;
  370. }
  371. static int i2c_pmic_parse_dt(struct i2c_pmic *chip)
  372. {
  373. struct device_node *node = chip->dev->of_node;
  374. int rc = 0, i;
  375. u32 temp;
  376. if (!node) {
  377. pr_err("missing device tree\n");
  378. return -EINVAL;
  379. }
  380. chip->num_periphs = of_property_count_u32_elems(node,
  381. "qcom,periph-map");
  382. if (chip->num_periphs < 0) {
  383. pr_err("missing qcom,periph-map property rc=%d\n",
  384. chip->num_periphs);
  385. return chip->num_periphs;
  386. }
  387. if (chip->num_periphs == 0) {
  388. pr_err("qcom,periph-map must contain at least one address\n");
  389. return -EINVAL;
  390. }
  391. chip->periph = devm_kcalloc(chip->dev, chip->num_periphs,
  392. sizeof(*chip->periph), GFP_KERNEL);
  393. if (!chip->periph)
  394. return -ENOMEM;
  395. for (i = 0; i < chip->num_periphs; i++) {
  396. rc = of_property_read_u32_index(node, "qcom,periph-map",
  397. i, &temp);
  398. if (rc < 0) {
  399. pr_err("Couldn't read qcom,periph-map[%d] rc=%d\n",
  400. i, rc);
  401. return rc;
  402. }
  403. chip->periph[i].addr = (u16)(temp << 8);
  404. chip->periph[i].data = chip;
  405. mutex_init(&chip->periph[i].lock);
  406. }
  407. of_property_read_string(node, "pinctrl-names", &chip->pinctrl_name);
  408. return rc;
  409. }
  410. #define MAX_I2C_RETRIES 3
  411. static int i2c_pmic_read(struct regmap *map, unsigned int reg, void *val,
  412. size_t val_count)
  413. {
  414. int rc, retries = 0;
  415. do {
  416. rc = regmap_bulk_read(map, reg, val, val_count);
  417. } while (rc == -ENOTCONN && retries++ < MAX_I2C_RETRIES);
  418. if (retries > 1)
  419. pr_err("i2c_pmic_read failed for %d retries, rc = %d\n",
  420. retries - 1, rc);
  421. return rc;
  422. }
  423. static int i2c_pmic_determine_initial_status(struct i2c_pmic *chip)
  424. {
  425. int rc, i;
  426. for (i = 0; i < chip->num_periphs; i++) {
  427. rc = i2c_pmic_read(chip->regmap,
  428. chip->periph[i].addr | INT_SET_TYPE_OFFSET,
  429. chip->periph[i].cached, IRQ_MAX_REGS);
  430. if (rc < 0) {
  431. pr_err("Couldn't read irq data rc=%d\n", rc);
  432. return rc;
  433. }
  434. memcpy(chip->periph[i].synced, chip->periph[i].cached,
  435. IRQ_MAX_REGS * sizeof(*chip->periph[i].synced));
  436. }
  437. return 0;
  438. }
  439. static struct regmap_config i2c_pmic_regmap_config = {
  440. .reg_bits = 16,
  441. .val_bits = 8,
  442. .max_register = 0xFFFF,
  443. };
  444. static int i2c_pmic_probe(struct i2c_client *client,
  445. const struct i2c_device_id *id)
  446. {
  447. struct i2c_pmic *chip;
  448. int rc = 0;
  449. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  450. if (!chip)
  451. return -ENOMEM;
  452. chip->dev = &client->dev;
  453. chip->regmap = devm_regmap_init_i2c(client, &i2c_pmic_regmap_config);
  454. if (!chip->regmap)
  455. return -ENODEV;
  456. i2c_set_clientdata(client, chip);
  457. if (!of_property_read_bool(chip->dev->of_node, "interrupt-controller"))
  458. goto probe_children;
  459. chip->domain = irq_domain_add_tree(client->dev.of_node,
  460. &i2c_pmic_domain_ops, chip);
  461. if (!chip->domain) {
  462. rc = -ENOMEM;
  463. goto cleanup;
  464. }
  465. rc = i2c_pmic_parse_dt(chip);
  466. if (rc < 0) {
  467. pr_err("Couldn't parse device tree rc=%d\n", rc);
  468. goto cleanup;
  469. }
  470. rc = i2c_pmic_determine_initial_status(chip);
  471. if (rc < 0) {
  472. pr_err("Couldn't determine initial status rc=%d\n", rc);
  473. goto cleanup;
  474. }
  475. if (chip->pinctrl_name) {
  476. chip->pinctrl = devm_pinctrl_get_select(chip->dev,
  477. chip->pinctrl_name);
  478. if (IS_ERR(chip->pinctrl)) {
  479. pr_err("Couldn't select %s pinctrl rc=%ld\n",
  480. chip->pinctrl_name, PTR_ERR(chip->pinctrl));
  481. rc = PTR_ERR(chip->pinctrl);
  482. goto cleanup;
  483. }
  484. }
  485. chip->resume_completed = true;
  486. mutex_init(&chip->irq_complete);
  487. rc = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  488. i2c_pmic_irq_handler,
  489. IRQF_ONESHOT | IRQF_SHARED,
  490. "i2c_pmic_stat_irq", chip);
  491. if (rc < 0) {
  492. pr_err("Couldn't request irq %d rc=%d\n", client->irq, rc);
  493. goto cleanup;
  494. }
  495. chip->summary_irq = client->irq;
  496. enable_irq_wake(client->irq);
  497. probe_children:
  498. of_platform_populate(chip->dev->of_node, NULL, NULL, chip->dev);
  499. pr_info("I2C PMIC probe successful\n");
  500. return rc;
  501. cleanup:
  502. if (chip->domain)
  503. irq_domain_remove(chip->domain);
  504. i2c_set_clientdata(client, NULL);
  505. return rc;
  506. }
  507. static int i2c_pmic_remove(struct i2c_client *client)
  508. {
  509. struct i2c_pmic *chip = i2c_get_clientdata(client);
  510. of_platform_depopulate(chip->dev);
  511. if (chip->domain)
  512. irq_domain_remove(chip->domain);
  513. i2c_set_clientdata(client, NULL);
  514. return 0;
  515. }
  516. #ifdef CONFIG_PM_SLEEP
  517. static int i2c_pmic_suspend_noirq(struct device *dev)
  518. {
  519. struct i2c_pmic *chip = dev_get_drvdata(dev);
  520. if (chip->irq_waiting) {
  521. pr_err_ratelimited("Aborting suspend, an interrupt was detected while suspending\n");
  522. return -EBUSY;
  523. }
  524. return 0;
  525. }
  526. static int i2c_pmic_suspend(struct device *dev)
  527. {
  528. struct i2c_pmic *chip = dev_get_drvdata(dev);
  529. struct i2c_pmic_periph *periph;
  530. int rc = 0, i;
  531. for (i = 0; i < chip->num_periphs; i++) {
  532. periph = &chip->periph[i];
  533. rc = regmap_write(chip->regmap,
  534. periph->addr | INT_EN_CLR_OFFSET, 0xFF);
  535. if (rc < 0) {
  536. pr_err_ratelimited("Couldn't clear 0x%04x irqs rc=%d\n",
  537. periph->addr, rc);
  538. continue;
  539. }
  540. rc = regmap_write(chip->regmap,
  541. periph->addr | INT_EN_SET_OFFSET,
  542. periph->wake);
  543. if (rc < 0)
  544. pr_err_ratelimited("Couldn't enable 0x%04x wake irqs 0x%02x rc=%d\n",
  545. periph->addr, periph->wake, rc);
  546. }
  547. if (!rc) {
  548. mutex_lock(&chip->irq_complete);
  549. chip->resume_completed = false;
  550. mutex_unlock(&chip->irq_complete);
  551. }
  552. return rc;
  553. }
  554. static int i2c_pmic_resume(struct device *dev)
  555. {
  556. struct i2c_pmic *chip = dev_get_drvdata(dev);
  557. struct i2c_pmic_periph *periph;
  558. int rc = 0, i;
  559. for (i = 0; i < chip->num_periphs; i++) {
  560. periph = &chip->periph[i];
  561. rc = regmap_write(chip->regmap,
  562. periph->addr | INT_EN_CLR_OFFSET, 0xFF);
  563. if (rc < 0) {
  564. pr_err("Couldn't clear 0x%04x irqs rc=%d\n",
  565. periph->addr, rc);
  566. continue;
  567. }
  568. rc = regmap_write(chip->regmap,
  569. periph->addr | INT_EN_SET_OFFSET,
  570. periph->synced[IRQ_EN_SET]);
  571. if (rc < 0)
  572. pr_err("Couldn't restore 0x%04x synced irqs 0x%02x rc=%d\n",
  573. periph->addr, periph->synced[IRQ_EN_SET], rc);
  574. }
  575. mutex_lock(&chip->irq_complete);
  576. chip->resume_completed = true;
  577. if (chip->irq_waiting) {
  578. mutex_unlock(&chip->irq_complete);
  579. /* irq was pending, call the handler */
  580. i2c_pmic_irq_handler(chip->summary_irq, chip);
  581. enable_irq(chip->summary_irq);
  582. } else {
  583. mutex_unlock(&chip->irq_complete);
  584. }
  585. return rc;
  586. }
  587. #else
  588. static int i2c_pmic_suspend(struct device *dev)
  589. {
  590. return 0;
  591. }
  592. static int i2c_pmic_resume(struct device *dev)
  593. {
  594. return 0;
  595. }
  596. static int i2c_pmic_suspend_noirq(struct device *dev)
  597. {
  598. return 0
  599. }
  600. #endif
  601. static const struct dev_pm_ops i2c_pmic_pm_ops = {
  602. .suspend = i2c_pmic_suspend,
  603. .suspend_noirq = i2c_pmic_suspend_noirq,
  604. .resume = i2c_pmic_resume,
  605. };
  606. static const struct of_device_id i2c_pmic_match_table[] = {
  607. { .compatible = "qcom,i2c-pmic", },
  608. { },
  609. };
  610. static const struct i2c_device_id i2c_pmic_id[] = {
  611. { "i2c-pmic", 0 },
  612. { },
  613. };
  614. MODULE_DEVICE_TABLE(i2c, i2c_pmic_id);
  615. static struct i2c_driver i2c_pmic_driver = {
  616. .driver = {
  617. .name = "i2c_pmic",
  618. .owner = THIS_MODULE,
  619. .pm = &i2c_pmic_pm_ops,
  620. .of_match_table = i2c_pmic_match_table,
  621. },
  622. .probe = i2c_pmic_probe,
  623. .remove = i2c_pmic_remove,
  624. .id_table = i2c_pmic_id,
  625. };
  626. module_i2c_driver(i2c_pmic_driver);
  627. MODULE_LICENSE("GPL v2");
  628. MODULE_ALIAS("i2c:i2c_pmic");