max8925-core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Base driver for Maxim MAX8925
  3. *
  4. * Copyright (C) 2009-2010 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  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 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/mfd/max8925.h>
  19. static struct resource io_parent = {
  20. .start = 0,
  21. .end = 0xffffffff,
  22. .flags = IORESOURCE_IO,
  23. };
  24. static struct resource backlight_resources[] = {
  25. {
  26. .name = "max8925-backlight",
  27. .start = MAX8925_WLED_MODE_CNTL,
  28. .end = MAX8925_WLED_CNTL,
  29. .flags = IORESOURCE_IO,
  30. .parent = &io_parent,
  31. },
  32. };
  33. static struct mfd_cell backlight_devs[] = {
  34. {
  35. .name = "max8925-backlight",
  36. .num_resources = 1,
  37. .resources = &backlight_resources[0],
  38. .id = -1,
  39. },
  40. };
  41. static struct resource touch_resources[] = {
  42. {
  43. .name = "max8925-tsc",
  44. .start = MAX8925_TSC_IRQ,
  45. .end = MAX8925_ADC_RES_END,
  46. .flags = IORESOURCE_IO,
  47. .parent = &io_parent,
  48. },
  49. };
  50. static struct mfd_cell touch_devs[] = {
  51. {
  52. .name = "max8925-touch",
  53. .num_resources = 1,
  54. .resources = &touch_resources[0],
  55. .id = -1,
  56. },
  57. };
  58. static struct resource power_supply_resources[] = {
  59. {
  60. .name = "max8925-power",
  61. .start = MAX8925_CHG_IRQ1,
  62. .end = MAX8925_CHG_IRQ1_MASK,
  63. .flags = IORESOURCE_IO,
  64. .parent = &io_parent,
  65. },
  66. };
  67. static struct mfd_cell power_devs[] = {
  68. {
  69. .name = "max8925-power",
  70. .num_resources = 1,
  71. .resources = &power_supply_resources[0],
  72. .id = -1,
  73. },
  74. };
  75. static struct resource rtc_resources[] = {
  76. {
  77. .name = "max8925-rtc",
  78. .start = MAX8925_RTC_IRQ,
  79. .end = MAX8925_RTC_IRQ_MASK,
  80. .flags = IORESOURCE_IO,
  81. },
  82. };
  83. static struct mfd_cell rtc_devs[] = {
  84. {
  85. .name = "max8925-rtc",
  86. .num_resources = 1,
  87. .resources = &rtc_resources[0],
  88. .id = -1,
  89. },
  90. };
  91. static struct resource onkey_resources[] = {
  92. {
  93. .name = "max8925-onkey",
  94. .start = MAX8925_IRQ_GPM_SW_R,
  95. .end = MAX8925_IRQ_GPM_SW_R,
  96. .flags = IORESOURCE_IRQ,
  97. }, {
  98. .name = "max8925-onkey",
  99. .start = MAX8925_IRQ_GPM_SW_F,
  100. .end = MAX8925_IRQ_GPM_SW_F,
  101. .flags = IORESOURCE_IRQ,
  102. },
  103. };
  104. static struct mfd_cell onkey_devs[] = {
  105. {
  106. .name = "max8925-onkey",
  107. .num_resources = 2,
  108. .resources = &onkey_resources[0],
  109. .id = -1,
  110. },
  111. };
  112. #define MAX8925_REG_RESOURCE(_start, _end) \
  113. { \
  114. .start = MAX8925_##_start, \
  115. .end = MAX8925_##_end, \
  116. .flags = IORESOURCE_IO, \
  117. .parent = &io_parent, \
  118. }
  119. static struct resource regulator_resources[] = {
  120. MAX8925_REG_RESOURCE(SDCTL1, SDCTL1),
  121. MAX8925_REG_RESOURCE(SDCTL2, SDCTL2),
  122. MAX8925_REG_RESOURCE(SDCTL3, SDCTL3),
  123. MAX8925_REG_RESOURCE(LDOCTL1, LDOCTL1),
  124. MAX8925_REG_RESOURCE(LDOCTL2, LDOCTL2),
  125. MAX8925_REG_RESOURCE(LDOCTL3, LDOCTL3),
  126. MAX8925_REG_RESOURCE(LDOCTL4, LDOCTL4),
  127. MAX8925_REG_RESOURCE(LDOCTL5, LDOCTL5),
  128. MAX8925_REG_RESOURCE(LDOCTL6, LDOCTL6),
  129. MAX8925_REG_RESOURCE(LDOCTL7, LDOCTL7),
  130. MAX8925_REG_RESOURCE(LDOCTL8, LDOCTL8),
  131. MAX8925_REG_RESOURCE(LDOCTL9, LDOCTL9),
  132. MAX8925_REG_RESOURCE(LDOCTL10, LDOCTL10),
  133. MAX8925_REG_RESOURCE(LDOCTL11, LDOCTL11),
  134. MAX8925_REG_RESOURCE(LDOCTL12, LDOCTL12),
  135. MAX8925_REG_RESOURCE(LDOCTL13, LDOCTL13),
  136. MAX8925_REG_RESOURCE(LDOCTL14, LDOCTL14),
  137. MAX8925_REG_RESOURCE(LDOCTL15, LDOCTL15),
  138. MAX8925_REG_RESOURCE(LDOCTL16, LDOCTL16),
  139. MAX8925_REG_RESOURCE(LDOCTL17, LDOCTL17),
  140. MAX8925_REG_RESOURCE(LDOCTL18, LDOCTL18),
  141. MAX8925_REG_RESOURCE(LDOCTL19, LDOCTL19),
  142. MAX8925_REG_RESOURCE(LDOCTL20, LDOCTL20),
  143. };
  144. #define MAX8925_REG_DEVS(_id) \
  145. { \
  146. .name = "max8925-regulator", \
  147. .num_resources = 1, \
  148. .resources = &regulator_resources[MAX8925_ID_##_id], \
  149. .id = MAX8925_ID_##_id, \
  150. }
  151. static struct mfd_cell regulator_devs[] = {
  152. MAX8925_REG_DEVS(SD1),
  153. MAX8925_REG_DEVS(SD2),
  154. MAX8925_REG_DEVS(SD3),
  155. MAX8925_REG_DEVS(LDO1),
  156. MAX8925_REG_DEVS(LDO2),
  157. MAX8925_REG_DEVS(LDO3),
  158. MAX8925_REG_DEVS(LDO4),
  159. MAX8925_REG_DEVS(LDO5),
  160. MAX8925_REG_DEVS(LDO6),
  161. MAX8925_REG_DEVS(LDO7),
  162. MAX8925_REG_DEVS(LDO8),
  163. MAX8925_REG_DEVS(LDO9),
  164. MAX8925_REG_DEVS(LDO10),
  165. MAX8925_REG_DEVS(LDO11),
  166. MAX8925_REG_DEVS(LDO12),
  167. MAX8925_REG_DEVS(LDO13),
  168. MAX8925_REG_DEVS(LDO14),
  169. MAX8925_REG_DEVS(LDO15),
  170. MAX8925_REG_DEVS(LDO16),
  171. MAX8925_REG_DEVS(LDO17),
  172. MAX8925_REG_DEVS(LDO18),
  173. MAX8925_REG_DEVS(LDO19),
  174. MAX8925_REG_DEVS(LDO20),
  175. };
  176. enum {
  177. FLAGS_ADC = 1, /* register in ADC component */
  178. FLAGS_RTC, /* register in RTC component */
  179. };
  180. struct max8925_irq_data {
  181. int reg;
  182. int mask_reg;
  183. int enable; /* enable or not */
  184. int offs; /* bit offset in mask register */
  185. int flags;
  186. int tsc_irq;
  187. };
  188. static struct max8925_irq_data max8925_irqs[] = {
  189. [MAX8925_IRQ_VCHG_DC_OVP] = {
  190. .reg = MAX8925_CHG_IRQ1,
  191. .mask_reg = MAX8925_CHG_IRQ1_MASK,
  192. .offs = 1 << 0,
  193. },
  194. [MAX8925_IRQ_VCHG_DC_F] = {
  195. .reg = MAX8925_CHG_IRQ1,
  196. .mask_reg = MAX8925_CHG_IRQ1_MASK,
  197. .offs = 1 << 1,
  198. },
  199. [MAX8925_IRQ_VCHG_DC_R] = {
  200. .reg = MAX8925_CHG_IRQ1,
  201. .mask_reg = MAX8925_CHG_IRQ1_MASK,
  202. .offs = 1 << 2,
  203. },
  204. [MAX8925_IRQ_VCHG_THM_OK_R] = {
  205. .reg = MAX8925_CHG_IRQ2,
  206. .mask_reg = MAX8925_CHG_IRQ2_MASK,
  207. .offs = 1 << 0,
  208. },
  209. [MAX8925_IRQ_VCHG_THM_OK_F] = {
  210. .reg = MAX8925_CHG_IRQ2,
  211. .mask_reg = MAX8925_CHG_IRQ2_MASK,
  212. .offs = 1 << 1,
  213. },
  214. [MAX8925_IRQ_VCHG_SYSLOW_F] = {
  215. .reg = MAX8925_CHG_IRQ2,
  216. .mask_reg = MAX8925_CHG_IRQ2_MASK,
  217. .offs = 1 << 2,
  218. },
  219. [MAX8925_IRQ_VCHG_SYSLOW_R] = {
  220. .reg = MAX8925_CHG_IRQ2,
  221. .mask_reg = MAX8925_CHG_IRQ2_MASK,
  222. .offs = 1 << 3,
  223. },
  224. [MAX8925_IRQ_VCHG_RST] = {
  225. .reg = MAX8925_CHG_IRQ2,
  226. .mask_reg = MAX8925_CHG_IRQ2_MASK,
  227. .offs = 1 << 4,
  228. },
  229. [MAX8925_IRQ_VCHG_DONE] = {
  230. .reg = MAX8925_CHG_IRQ2,
  231. .mask_reg = MAX8925_CHG_IRQ2_MASK,
  232. .offs = 1 << 5,
  233. },
  234. [MAX8925_IRQ_VCHG_TOPOFF] = {
  235. .reg = MAX8925_CHG_IRQ2,
  236. .mask_reg = MAX8925_CHG_IRQ2_MASK,
  237. .offs = 1 << 6,
  238. },
  239. [MAX8925_IRQ_VCHG_TMR_FAULT] = {
  240. .reg = MAX8925_CHG_IRQ2,
  241. .mask_reg = MAX8925_CHG_IRQ2_MASK,
  242. .offs = 1 << 7,
  243. },
  244. [MAX8925_IRQ_GPM_RSTIN] = {
  245. .reg = MAX8925_ON_OFF_IRQ1,
  246. .mask_reg = MAX8925_ON_OFF_IRQ1_MASK,
  247. .offs = 1 << 0,
  248. },
  249. [MAX8925_IRQ_GPM_MPL] = {
  250. .reg = MAX8925_ON_OFF_IRQ1,
  251. .mask_reg = MAX8925_ON_OFF_IRQ1_MASK,
  252. .offs = 1 << 1,
  253. },
  254. [MAX8925_IRQ_GPM_SW_3SEC] = {
  255. .reg = MAX8925_ON_OFF_IRQ1,
  256. .mask_reg = MAX8925_ON_OFF_IRQ1_MASK,
  257. .offs = 1 << 2,
  258. },
  259. [MAX8925_IRQ_GPM_EXTON_F] = {
  260. .reg = MAX8925_ON_OFF_IRQ1,
  261. .mask_reg = MAX8925_ON_OFF_IRQ1_MASK,
  262. .offs = 1 << 3,
  263. },
  264. [MAX8925_IRQ_GPM_EXTON_R] = {
  265. .reg = MAX8925_ON_OFF_IRQ1,
  266. .mask_reg = MAX8925_ON_OFF_IRQ1_MASK,
  267. .offs = 1 << 4,
  268. },
  269. [MAX8925_IRQ_GPM_SW_1SEC] = {
  270. .reg = MAX8925_ON_OFF_IRQ1,
  271. .mask_reg = MAX8925_ON_OFF_IRQ1_MASK,
  272. .offs = 1 << 5,
  273. },
  274. [MAX8925_IRQ_GPM_SW_F] = {
  275. .reg = MAX8925_ON_OFF_IRQ1,
  276. .mask_reg = MAX8925_ON_OFF_IRQ1_MASK,
  277. .offs = 1 << 6,
  278. },
  279. [MAX8925_IRQ_GPM_SW_R] = {
  280. .reg = MAX8925_ON_OFF_IRQ1,
  281. .mask_reg = MAX8925_ON_OFF_IRQ1_MASK,
  282. .offs = 1 << 7,
  283. },
  284. [MAX8925_IRQ_GPM_SYSCKEN_F] = {
  285. .reg = MAX8925_ON_OFF_IRQ2,
  286. .mask_reg = MAX8925_ON_OFF_IRQ2_MASK,
  287. .offs = 1 << 0,
  288. },
  289. [MAX8925_IRQ_GPM_SYSCKEN_R] = {
  290. .reg = MAX8925_ON_OFF_IRQ2,
  291. .mask_reg = MAX8925_ON_OFF_IRQ2_MASK,
  292. .offs = 1 << 1,
  293. },
  294. [MAX8925_IRQ_RTC_ALARM1] = {
  295. .reg = MAX8925_RTC_IRQ,
  296. .mask_reg = MAX8925_RTC_IRQ_MASK,
  297. .offs = 1 << 2,
  298. .flags = FLAGS_RTC,
  299. },
  300. [MAX8925_IRQ_RTC_ALARM0] = {
  301. .reg = MAX8925_RTC_IRQ,
  302. .mask_reg = MAX8925_RTC_IRQ_MASK,
  303. .offs = 1 << 3,
  304. .flags = FLAGS_RTC,
  305. },
  306. [MAX8925_IRQ_TSC_STICK] = {
  307. .reg = MAX8925_TSC_IRQ,
  308. .mask_reg = MAX8925_TSC_IRQ_MASK,
  309. .offs = 1 << 0,
  310. .flags = FLAGS_ADC,
  311. .tsc_irq = 1,
  312. },
  313. [MAX8925_IRQ_TSC_NSTICK] = {
  314. .reg = MAX8925_TSC_IRQ,
  315. .mask_reg = MAX8925_TSC_IRQ_MASK,
  316. .offs = 1 << 1,
  317. .flags = FLAGS_ADC,
  318. .tsc_irq = 1,
  319. },
  320. };
  321. static inline struct max8925_irq_data *irq_to_max8925(struct max8925_chip *chip,
  322. int irq)
  323. {
  324. return &max8925_irqs[irq - chip->irq_base];
  325. }
  326. static irqreturn_t max8925_irq(int irq, void *data)
  327. {
  328. struct max8925_chip *chip = data;
  329. struct max8925_irq_data *irq_data;
  330. struct i2c_client *i2c;
  331. int read_reg = -1, value = 0;
  332. int i;
  333. for (i = 0; i < ARRAY_SIZE(max8925_irqs); i++) {
  334. irq_data = &max8925_irqs[i];
  335. /* TSC IRQ should be serviced in max8925_tsc_irq() */
  336. if (irq_data->tsc_irq)
  337. continue;
  338. if (irq_data->flags == FLAGS_RTC)
  339. i2c = chip->rtc;
  340. else if (irq_data->flags == FLAGS_ADC)
  341. i2c = chip->adc;
  342. else
  343. i2c = chip->i2c;
  344. if (read_reg != irq_data->reg) {
  345. read_reg = irq_data->reg;
  346. value = max8925_reg_read(i2c, irq_data->reg);
  347. }
  348. if (value & irq_data->enable)
  349. handle_nested_irq(chip->irq_base + i);
  350. }
  351. return IRQ_HANDLED;
  352. }
  353. static irqreturn_t max8925_tsc_irq(int irq, void *data)
  354. {
  355. struct max8925_chip *chip = data;
  356. struct max8925_irq_data *irq_data;
  357. struct i2c_client *i2c;
  358. int read_reg = -1, value = 0;
  359. int i;
  360. for (i = 0; i < ARRAY_SIZE(max8925_irqs); i++) {
  361. irq_data = &max8925_irqs[i];
  362. /* non TSC IRQ should be serviced in max8925_irq() */
  363. if (!irq_data->tsc_irq)
  364. continue;
  365. if (irq_data->flags == FLAGS_RTC)
  366. i2c = chip->rtc;
  367. else if (irq_data->flags == FLAGS_ADC)
  368. i2c = chip->adc;
  369. else
  370. i2c = chip->i2c;
  371. if (read_reg != irq_data->reg) {
  372. read_reg = irq_data->reg;
  373. value = max8925_reg_read(i2c, irq_data->reg);
  374. }
  375. if (value & irq_data->enable)
  376. handle_nested_irq(chip->irq_base + i);
  377. }
  378. return IRQ_HANDLED;
  379. }
  380. static void max8925_irq_lock(struct irq_data *data)
  381. {
  382. struct max8925_chip *chip = irq_data_get_irq_chip_data(data);
  383. mutex_lock(&chip->irq_lock);
  384. }
  385. static void max8925_irq_sync_unlock(struct irq_data *data)
  386. {
  387. struct max8925_chip *chip = irq_data_get_irq_chip_data(data);
  388. struct max8925_irq_data *irq_data;
  389. static unsigned char cache_chg[2] = {0xff, 0xff};
  390. static unsigned char cache_on[2] = {0xff, 0xff};
  391. static unsigned char cache_rtc = 0xff, cache_tsc = 0xff;
  392. unsigned char irq_chg[2], irq_on[2];
  393. unsigned char irq_rtc, irq_tsc;
  394. int i;
  395. /* Load cached value. In initial, all IRQs are masked */
  396. irq_chg[0] = cache_chg[0];
  397. irq_chg[1] = cache_chg[1];
  398. irq_on[0] = cache_on[0];
  399. irq_on[1] = cache_on[1];
  400. irq_rtc = cache_rtc;
  401. irq_tsc = cache_tsc;
  402. for (i = 0; i < ARRAY_SIZE(max8925_irqs); i++) {
  403. irq_data = &max8925_irqs[i];
  404. /* 1 -- disable, 0 -- enable */
  405. switch (irq_data->mask_reg) {
  406. case MAX8925_CHG_IRQ1_MASK:
  407. irq_chg[0] &= ~irq_data->enable;
  408. break;
  409. case MAX8925_CHG_IRQ2_MASK:
  410. irq_chg[1] &= ~irq_data->enable;
  411. break;
  412. case MAX8925_ON_OFF_IRQ1_MASK:
  413. irq_on[0] &= ~irq_data->enable;
  414. break;
  415. case MAX8925_ON_OFF_IRQ2_MASK:
  416. irq_on[1] &= ~irq_data->enable;
  417. break;
  418. case MAX8925_RTC_IRQ_MASK:
  419. irq_rtc &= ~irq_data->enable;
  420. break;
  421. case MAX8925_TSC_IRQ_MASK:
  422. irq_tsc &= ~irq_data->enable;
  423. break;
  424. default:
  425. dev_err(chip->dev, "wrong IRQ\n");
  426. break;
  427. }
  428. }
  429. /* update mask into registers */
  430. if (cache_chg[0] != irq_chg[0]) {
  431. cache_chg[0] = irq_chg[0];
  432. max8925_reg_write(chip->i2c, MAX8925_CHG_IRQ1_MASK,
  433. irq_chg[0]);
  434. }
  435. if (cache_chg[1] != irq_chg[1]) {
  436. cache_chg[1] = irq_chg[1];
  437. max8925_reg_write(chip->i2c, MAX8925_CHG_IRQ2_MASK,
  438. irq_chg[1]);
  439. }
  440. if (cache_on[0] != irq_on[0]) {
  441. cache_on[0] = irq_on[0];
  442. max8925_reg_write(chip->i2c, MAX8925_ON_OFF_IRQ1_MASK,
  443. irq_on[0]);
  444. }
  445. if (cache_on[1] != irq_on[1]) {
  446. cache_on[1] = irq_on[1];
  447. max8925_reg_write(chip->i2c, MAX8925_ON_OFF_IRQ2_MASK,
  448. irq_on[1]);
  449. }
  450. if (cache_rtc != irq_rtc) {
  451. cache_rtc = irq_rtc;
  452. max8925_reg_write(chip->rtc, MAX8925_RTC_IRQ_MASK, irq_rtc);
  453. }
  454. if (cache_tsc != irq_tsc) {
  455. cache_tsc = irq_tsc;
  456. max8925_reg_write(chip->adc, MAX8925_TSC_IRQ_MASK, irq_tsc);
  457. }
  458. mutex_unlock(&chip->irq_lock);
  459. }
  460. static void max8925_irq_enable(struct irq_data *data)
  461. {
  462. struct max8925_chip *chip = irq_data_get_irq_chip_data(data);
  463. max8925_irqs[data->irq - chip->irq_base].enable
  464. = max8925_irqs[data->irq - chip->irq_base].offs;
  465. }
  466. static void max8925_irq_disable(struct irq_data *data)
  467. {
  468. struct max8925_chip *chip = irq_data_get_irq_chip_data(data);
  469. max8925_irqs[data->irq - chip->irq_base].enable = 0;
  470. }
  471. static struct irq_chip max8925_irq_chip = {
  472. .name = "max8925",
  473. .irq_bus_lock = max8925_irq_lock,
  474. .irq_bus_sync_unlock = max8925_irq_sync_unlock,
  475. .irq_enable = max8925_irq_enable,
  476. .irq_disable = max8925_irq_disable,
  477. };
  478. static int max8925_irq_init(struct max8925_chip *chip, int irq,
  479. struct max8925_platform_data *pdata)
  480. {
  481. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  482. int i, ret;
  483. int __irq;
  484. if (!pdata || !pdata->irq_base) {
  485. dev_warn(chip->dev, "No interrupt support on IRQ base\n");
  486. return -EINVAL;
  487. }
  488. /* clear all interrupts */
  489. max8925_reg_read(chip->i2c, MAX8925_CHG_IRQ1);
  490. max8925_reg_read(chip->i2c, MAX8925_CHG_IRQ2);
  491. max8925_reg_read(chip->i2c, MAX8925_ON_OFF_IRQ1);
  492. max8925_reg_read(chip->i2c, MAX8925_ON_OFF_IRQ2);
  493. max8925_reg_read(chip->rtc, MAX8925_RTC_IRQ);
  494. max8925_reg_read(chip->adc, MAX8925_TSC_IRQ);
  495. /* mask all interrupts except for TSC */
  496. max8925_reg_write(chip->rtc, MAX8925_ALARM0_CNTL, 0);
  497. max8925_reg_write(chip->rtc, MAX8925_ALARM1_CNTL, 0);
  498. max8925_reg_write(chip->i2c, MAX8925_CHG_IRQ1_MASK, 0xff);
  499. max8925_reg_write(chip->i2c, MAX8925_CHG_IRQ2_MASK, 0xff);
  500. max8925_reg_write(chip->i2c, MAX8925_ON_OFF_IRQ1_MASK, 0xff);
  501. max8925_reg_write(chip->i2c, MAX8925_ON_OFF_IRQ2_MASK, 0xff);
  502. max8925_reg_write(chip->rtc, MAX8925_RTC_IRQ_MASK, 0xff);
  503. mutex_init(&chip->irq_lock);
  504. chip->core_irq = irq;
  505. chip->irq_base = pdata->irq_base;
  506. /* register with genirq */
  507. for (i = 0; i < ARRAY_SIZE(max8925_irqs); i++) {
  508. __irq = i + chip->irq_base;
  509. irq_set_chip_data(__irq, chip);
  510. irq_set_chip_and_handler(__irq, &max8925_irq_chip,
  511. handle_edge_irq);
  512. irq_set_nested_thread(__irq, 1);
  513. #ifdef CONFIG_ARM
  514. set_irq_flags(__irq, IRQF_VALID);
  515. #else
  516. irq_set_noprobe(__irq);
  517. #endif
  518. }
  519. if (!irq) {
  520. dev_warn(chip->dev, "No interrupt support on core IRQ\n");
  521. goto tsc_irq;
  522. }
  523. ret = request_threaded_irq(irq, NULL, max8925_irq, flags,
  524. "max8925", chip);
  525. if (ret) {
  526. dev_err(chip->dev, "Failed to request core IRQ: %d\n", ret);
  527. chip->core_irq = 0;
  528. }
  529. tsc_irq:
  530. /* mask TSC interrupt */
  531. max8925_reg_write(chip->adc, MAX8925_TSC_IRQ_MASK, 0x0f);
  532. if (!pdata->tsc_irq) {
  533. dev_warn(chip->dev, "No interrupt support on TSC IRQ\n");
  534. return 0;
  535. }
  536. chip->tsc_irq = pdata->tsc_irq;
  537. ret = request_threaded_irq(chip->tsc_irq, NULL, max8925_tsc_irq,
  538. flags, "max8925-tsc", chip);
  539. if (ret) {
  540. dev_err(chip->dev, "Failed to request TSC IRQ: %d\n", ret);
  541. chip->tsc_irq = 0;
  542. }
  543. return 0;
  544. }
  545. int __devinit max8925_device_init(struct max8925_chip *chip,
  546. struct max8925_platform_data *pdata)
  547. {
  548. int ret;
  549. max8925_irq_init(chip, chip->i2c->irq, pdata);
  550. if (pdata && (pdata->power || pdata->touch)) {
  551. /* enable ADC to control internal reference */
  552. max8925_set_bits(chip->i2c, MAX8925_RESET_CNFG, 1, 1);
  553. /* enable internal reference for ADC */
  554. max8925_set_bits(chip->adc, MAX8925_TSC_CNFG1, 3, 2);
  555. /* check for internal reference IRQ */
  556. do {
  557. ret = max8925_reg_read(chip->adc, MAX8925_TSC_IRQ);
  558. } while (ret & MAX8925_NREF_OK);
  559. /* enaable ADC scheduler, interval is 1 second */
  560. max8925_set_bits(chip->adc, MAX8925_ADC_SCHED, 3, 2);
  561. }
  562. /* enable Momentary Power Loss */
  563. max8925_set_bits(chip->rtc, MAX8925_MPL_CNTL, 1 << 4, 1 << 4);
  564. ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
  565. ARRAY_SIZE(rtc_devs),
  566. &rtc_resources[0], 0);
  567. if (ret < 0) {
  568. dev_err(chip->dev, "Failed to add rtc subdev\n");
  569. goto out;
  570. }
  571. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  572. ARRAY_SIZE(onkey_devs),
  573. &onkey_resources[0], 0);
  574. if (ret < 0) {
  575. dev_err(chip->dev, "Failed to add onkey subdev\n");
  576. goto out_dev;
  577. }
  578. if (pdata) {
  579. ret = mfd_add_devices(chip->dev, 0, &regulator_devs[0],
  580. ARRAY_SIZE(regulator_devs),
  581. &regulator_resources[0], 0);
  582. if (ret < 0) {
  583. dev_err(chip->dev, "Failed to add regulator subdev\n");
  584. goto out_dev;
  585. }
  586. }
  587. if (pdata && pdata->backlight) {
  588. ret = mfd_add_devices(chip->dev, 0, &backlight_devs[0],
  589. ARRAY_SIZE(backlight_devs),
  590. &backlight_resources[0], 0);
  591. if (ret < 0) {
  592. dev_err(chip->dev, "Failed to add backlight subdev\n");
  593. goto out_dev;
  594. }
  595. }
  596. if (pdata && pdata->power) {
  597. ret = mfd_add_devices(chip->dev, 0, &power_devs[0],
  598. ARRAY_SIZE(power_devs),
  599. &power_supply_resources[0], 0);
  600. if (ret < 0) {
  601. dev_err(chip->dev, "Failed to add power supply "
  602. "subdev\n");
  603. goto out_dev;
  604. }
  605. }
  606. if (pdata && pdata->touch) {
  607. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  608. ARRAY_SIZE(touch_devs),
  609. &touch_resources[0], 0);
  610. if (ret < 0) {
  611. dev_err(chip->dev, "Failed to add touch subdev\n");
  612. goto out_dev;
  613. }
  614. }
  615. return 0;
  616. out_dev:
  617. mfd_remove_devices(chip->dev);
  618. out:
  619. return ret;
  620. }
  621. void __devexit max8925_device_exit(struct max8925_chip *chip)
  622. {
  623. if (chip->core_irq)
  624. free_irq(chip->core_irq, chip);
  625. if (chip->tsc_irq)
  626. free_irq(chip->tsc_irq, chip);
  627. mfd_remove_devices(chip->dev);
  628. }
  629. MODULE_DESCRIPTION("PMIC Driver for Maxim MAX8925");
  630. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com");
  631. MODULE_LICENSE("GPL");