88pm860x-core.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. * Base driver for Marvell 88PM8607
  3. *
  4. * Copyright (C) 2009 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/88pm860x.h>
  19. #include <linux/regulator/machine.h>
  20. #define INT_STATUS_NUM 3
  21. static struct resource bk0_resources[] __devinitdata = {
  22. {2, 2, "duty cycle", IORESOURCE_REG, },
  23. {3, 3, "always on", IORESOURCE_REG, },
  24. {3, 3, "current", IORESOURCE_REG, },
  25. };
  26. static struct resource bk1_resources[] __devinitdata = {
  27. {4, 4, "duty cycle", IORESOURCE_REG, },
  28. {5, 5, "always on", IORESOURCE_REG, },
  29. {5, 5, "current", IORESOURCE_REG, },
  30. };
  31. static struct resource bk2_resources[] __devinitdata = {
  32. {6, 6, "duty cycle", IORESOURCE_REG, },
  33. {7, 7, "always on", IORESOURCE_REG, },
  34. {5, 5, "current", IORESOURCE_REG, },
  35. };
  36. static struct resource led0_resources[] __devinitdata = {
  37. /* RGB1 Red LED */
  38. {0xd, 0xd, "control", IORESOURCE_REG, },
  39. {0xc, 0xc, "blink", IORESOURCE_REG, },
  40. };
  41. static struct resource led1_resources[] __devinitdata = {
  42. /* RGB1 Green LED */
  43. {0xe, 0xe, "control", IORESOURCE_REG, },
  44. {0xc, 0xc, "blink", IORESOURCE_REG, },
  45. };
  46. static struct resource led2_resources[] __devinitdata = {
  47. /* RGB1 Blue LED */
  48. {0xf, 0xf, "control", IORESOURCE_REG, },
  49. {0xc, 0xc, "blink", IORESOURCE_REG, },
  50. };
  51. static struct resource led3_resources[] __devinitdata = {
  52. /* RGB2 Red LED */
  53. {0x9, 0x9, "control", IORESOURCE_REG, },
  54. {0x8, 0x8, "blink", IORESOURCE_REG, },
  55. };
  56. static struct resource led4_resources[] __devinitdata = {
  57. /* RGB2 Green LED */
  58. {0xa, 0xa, "control", IORESOURCE_REG, },
  59. {0x8, 0x8, "blink", IORESOURCE_REG, },
  60. };
  61. static struct resource led5_resources[] __devinitdata = {
  62. /* RGB2 Blue LED */
  63. {0xb, 0xb, "control", IORESOURCE_REG, },
  64. {0x8, 0x8, "blink", IORESOURCE_REG, },
  65. };
  66. static struct resource buck1_resources[] __devinitdata = {
  67. {0x24, 0x24, "buck set", IORESOURCE_REG, },
  68. };
  69. static struct resource buck2_resources[] __devinitdata = {
  70. {0x25, 0x25, "buck set", IORESOURCE_REG, },
  71. };
  72. static struct resource buck3_resources[] __devinitdata = {
  73. {0x26, 0x26, "buck set", IORESOURCE_REG, },
  74. };
  75. static struct resource ldo1_resources[] __devinitdata = {
  76. {0x10, 0x10, "ldo set", IORESOURCE_REG, },
  77. };
  78. static struct resource ldo2_resources[] __devinitdata = {
  79. {0x11, 0x11, "ldo set", IORESOURCE_REG, },
  80. };
  81. static struct resource ldo3_resources[] __devinitdata = {
  82. {0x12, 0x12, "ldo set", IORESOURCE_REG, },
  83. };
  84. static struct resource ldo4_resources[] __devinitdata = {
  85. {0x13, 0x13, "ldo set", IORESOURCE_REG, },
  86. };
  87. static struct resource ldo5_resources[] __devinitdata = {
  88. {0x14, 0x14, "ldo set", IORESOURCE_REG, },
  89. };
  90. static struct resource ldo6_resources[] __devinitdata = {
  91. {0x15, 0x15, "ldo set", IORESOURCE_REG, },
  92. };
  93. static struct resource ldo7_resources[] __devinitdata = {
  94. {0x16, 0x16, "ldo set", IORESOURCE_REG, },
  95. };
  96. static struct resource ldo8_resources[] __devinitdata = {
  97. {0x17, 0x17, "ldo set", IORESOURCE_REG, },
  98. };
  99. static struct resource ldo9_resources[] __devinitdata = {
  100. {0x18, 0x18, "ldo set", IORESOURCE_REG, },
  101. };
  102. static struct resource ldo10_resources[] __devinitdata = {
  103. {0x19, 0x19, "ldo set", IORESOURCE_REG, },
  104. };
  105. static struct resource ldo12_resources[] __devinitdata = {
  106. {0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
  107. };
  108. static struct resource ldo_vibrator_resources[] __devinitdata = {
  109. {0x28, 0x28, "ldo set", IORESOURCE_REG, },
  110. };
  111. static struct resource ldo14_resources[] __devinitdata = {
  112. {0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
  113. };
  114. static struct resource touch_resources[] __devinitdata = {
  115. {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
  116. };
  117. static struct resource onkey_resources[] __devinitdata = {
  118. {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
  119. };
  120. static struct resource codec_resources[] __devinitdata = {
  121. /* Headset microphone insertion or removal */
  122. {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
  123. /* Hook-switch press or release */
  124. {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
  125. /* Headset insertion or removal */
  126. {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
  127. /* Audio short */
  128. {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
  129. };
  130. static struct resource battery_resources[] __devinitdata = {
  131. {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
  132. {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
  133. };
  134. static struct resource charger_resources[] __devinitdata = {
  135. {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
  136. {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,},
  137. {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout", IORESOURCE_IRQ,},
  138. {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,},
  139. {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
  140. {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
  141. };
  142. static struct resource preg_resources[] __devinitdata = {
  143. {PM8606_ID_PREG, PM8606_ID_PREG, "preg", IORESOURCE_REG,},
  144. };
  145. static struct resource rtc_resources[] __devinitdata = {
  146. {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
  147. };
  148. static struct mfd_cell bk_devs[] = {
  149. {
  150. .name = "88pm860x-backlight",
  151. .id = 0,
  152. .num_resources = ARRAY_SIZE(bk0_resources),
  153. .resources = bk0_resources,
  154. }, {
  155. .name = "88pm860x-backlight",
  156. .id = 1,
  157. .num_resources = ARRAY_SIZE(bk1_resources),
  158. .resources = bk1_resources,
  159. }, {
  160. .name = "88pm860x-backlight",
  161. .id = 2,
  162. .num_resources = ARRAY_SIZE(bk2_resources),
  163. .resources = bk2_resources,
  164. },
  165. };
  166. static struct mfd_cell led_devs[] = {
  167. {
  168. .name = "88pm860x-led",
  169. .id = 0,
  170. .num_resources = ARRAY_SIZE(led0_resources),
  171. .resources = led0_resources,
  172. }, {
  173. .name = "88pm860x-led",
  174. .id = 1,
  175. .num_resources = ARRAY_SIZE(led1_resources),
  176. .resources = led1_resources,
  177. }, {
  178. .name = "88pm860x-led",
  179. .id = 2,
  180. .num_resources = ARRAY_SIZE(led2_resources),
  181. .resources = led2_resources,
  182. }, {
  183. .name = "88pm860x-led",
  184. .id = 3,
  185. .num_resources = ARRAY_SIZE(led3_resources),
  186. .resources = led3_resources,
  187. }, {
  188. .name = "88pm860x-led",
  189. .id = 4,
  190. .num_resources = ARRAY_SIZE(led4_resources),
  191. .resources = led4_resources,
  192. }, {
  193. .name = "88pm860x-led",
  194. .id = 5,
  195. .num_resources = ARRAY_SIZE(led5_resources),
  196. .resources = led5_resources,
  197. },
  198. };
  199. static struct mfd_cell reg_devs[] = {
  200. {
  201. .name = "88pm860x-regulator",
  202. .id = 0,
  203. .num_resources = ARRAY_SIZE(buck1_resources),
  204. .resources = buck1_resources,
  205. }, {
  206. .name = "88pm860x-regulator",
  207. .id = 1,
  208. .num_resources = ARRAY_SIZE(buck2_resources),
  209. .resources = buck2_resources,
  210. }, {
  211. .name = "88pm860x-regulator",
  212. .id = 2,
  213. .num_resources = ARRAY_SIZE(buck3_resources),
  214. .resources = buck3_resources,
  215. }, {
  216. .name = "88pm860x-regulator",
  217. .id = 3,
  218. .num_resources = ARRAY_SIZE(ldo1_resources),
  219. .resources = ldo1_resources,
  220. }, {
  221. .name = "88pm860x-regulator",
  222. .id = 4,
  223. .num_resources = ARRAY_SIZE(ldo2_resources),
  224. .resources = ldo2_resources,
  225. }, {
  226. .name = "88pm860x-regulator",
  227. .id = 5,
  228. .num_resources = ARRAY_SIZE(ldo3_resources),
  229. .resources = ldo3_resources,
  230. }, {
  231. .name = "88pm860x-regulator",
  232. .id = 6,
  233. .num_resources = ARRAY_SIZE(ldo4_resources),
  234. .resources = ldo4_resources,
  235. }, {
  236. .name = "88pm860x-regulator",
  237. .id = 7,
  238. .num_resources = ARRAY_SIZE(ldo5_resources),
  239. .resources = ldo5_resources,
  240. }, {
  241. .name = "88pm860x-regulator",
  242. .id = 8,
  243. .num_resources = ARRAY_SIZE(ldo6_resources),
  244. .resources = ldo6_resources,
  245. }, {
  246. .name = "88pm860x-regulator",
  247. .id = 9,
  248. .num_resources = ARRAY_SIZE(ldo7_resources),
  249. .resources = ldo7_resources,
  250. }, {
  251. .name = "88pm860x-regulator",
  252. .id = 10,
  253. .num_resources = ARRAY_SIZE(ldo8_resources),
  254. .resources = ldo8_resources,
  255. }, {
  256. .name = "88pm860x-regulator",
  257. .id = 11,
  258. .num_resources = ARRAY_SIZE(ldo9_resources),
  259. .resources = ldo9_resources,
  260. }, {
  261. .name = "88pm860x-regulator",
  262. .id = 12,
  263. .num_resources = ARRAY_SIZE(ldo10_resources),
  264. .resources = ldo10_resources,
  265. }, {
  266. .name = "88pm860x-regulator",
  267. .id = 13,
  268. .num_resources = ARRAY_SIZE(ldo12_resources),
  269. .resources = ldo12_resources,
  270. }, {
  271. .name = "88pm860x-regulator",
  272. .id = 14,
  273. .num_resources = ARRAY_SIZE(ldo_vibrator_resources),
  274. .resources = ldo_vibrator_resources,
  275. }, {
  276. .name = "88pm860x-regulator",
  277. .id = 15,
  278. .num_resources = ARRAY_SIZE(ldo14_resources),
  279. .resources = ldo14_resources,
  280. },
  281. };
  282. static struct mfd_cell touch_devs[] = {
  283. {"88pm860x-touch", -1,},
  284. };
  285. static struct mfd_cell onkey_devs[] = {
  286. {"88pm860x-onkey", -1,},
  287. };
  288. static struct mfd_cell codec_devs[] = {
  289. {"88pm860x-codec", -1,},
  290. };
  291. static struct regulator_consumer_supply preg_supply[] = {
  292. REGULATOR_SUPPLY("preg", "charger-manager"),
  293. };
  294. static struct regulator_init_data preg_init_data = {
  295. .num_consumer_supplies = ARRAY_SIZE(preg_supply),
  296. .consumer_supplies = &preg_supply[0],
  297. };
  298. static struct mfd_cell power_devs[] = {
  299. {"88pm860x-battery", -1,},
  300. {"88pm860x-charger", -1,},
  301. {"88pm860x-preg", -1,},
  302. };
  303. static struct mfd_cell rtc_devs[] = {
  304. {"88pm860x-rtc", -1,},
  305. };
  306. struct pm860x_irq_data {
  307. int reg;
  308. int mask_reg;
  309. int enable; /* enable or not */
  310. int offs; /* bit offset in mask register */
  311. };
  312. static struct pm860x_irq_data pm860x_irqs[] = {
  313. [PM8607_IRQ_ONKEY] = {
  314. .reg = PM8607_INT_STATUS1,
  315. .mask_reg = PM8607_INT_MASK_1,
  316. .offs = 1 << 0,
  317. },
  318. [PM8607_IRQ_EXTON] = {
  319. .reg = PM8607_INT_STATUS1,
  320. .mask_reg = PM8607_INT_MASK_1,
  321. .offs = 1 << 1,
  322. },
  323. [PM8607_IRQ_CHG] = {
  324. .reg = PM8607_INT_STATUS1,
  325. .mask_reg = PM8607_INT_MASK_1,
  326. .offs = 1 << 2,
  327. },
  328. [PM8607_IRQ_BAT] = {
  329. .reg = PM8607_INT_STATUS1,
  330. .mask_reg = PM8607_INT_MASK_1,
  331. .offs = 1 << 3,
  332. },
  333. [PM8607_IRQ_RTC] = {
  334. .reg = PM8607_INT_STATUS1,
  335. .mask_reg = PM8607_INT_MASK_1,
  336. .offs = 1 << 4,
  337. },
  338. [PM8607_IRQ_CC] = {
  339. .reg = PM8607_INT_STATUS1,
  340. .mask_reg = PM8607_INT_MASK_1,
  341. .offs = 1 << 5,
  342. },
  343. [PM8607_IRQ_VBAT] = {
  344. .reg = PM8607_INT_STATUS2,
  345. .mask_reg = PM8607_INT_MASK_2,
  346. .offs = 1 << 0,
  347. },
  348. [PM8607_IRQ_VCHG] = {
  349. .reg = PM8607_INT_STATUS2,
  350. .mask_reg = PM8607_INT_MASK_2,
  351. .offs = 1 << 1,
  352. },
  353. [PM8607_IRQ_VSYS] = {
  354. .reg = PM8607_INT_STATUS2,
  355. .mask_reg = PM8607_INT_MASK_2,
  356. .offs = 1 << 2,
  357. },
  358. [PM8607_IRQ_TINT] = {
  359. .reg = PM8607_INT_STATUS2,
  360. .mask_reg = PM8607_INT_MASK_2,
  361. .offs = 1 << 3,
  362. },
  363. [PM8607_IRQ_GPADC0] = {
  364. .reg = PM8607_INT_STATUS2,
  365. .mask_reg = PM8607_INT_MASK_2,
  366. .offs = 1 << 4,
  367. },
  368. [PM8607_IRQ_GPADC1] = {
  369. .reg = PM8607_INT_STATUS2,
  370. .mask_reg = PM8607_INT_MASK_2,
  371. .offs = 1 << 5,
  372. },
  373. [PM8607_IRQ_GPADC2] = {
  374. .reg = PM8607_INT_STATUS2,
  375. .mask_reg = PM8607_INT_MASK_2,
  376. .offs = 1 << 6,
  377. },
  378. [PM8607_IRQ_GPADC3] = {
  379. .reg = PM8607_INT_STATUS2,
  380. .mask_reg = PM8607_INT_MASK_2,
  381. .offs = 1 << 7,
  382. },
  383. [PM8607_IRQ_AUDIO_SHORT] = {
  384. .reg = PM8607_INT_STATUS3,
  385. .mask_reg = PM8607_INT_MASK_3,
  386. .offs = 1 << 0,
  387. },
  388. [PM8607_IRQ_PEN] = {
  389. .reg = PM8607_INT_STATUS3,
  390. .mask_reg = PM8607_INT_MASK_3,
  391. .offs = 1 << 1,
  392. },
  393. [PM8607_IRQ_HEADSET] = {
  394. .reg = PM8607_INT_STATUS3,
  395. .mask_reg = PM8607_INT_MASK_3,
  396. .offs = 1 << 2,
  397. },
  398. [PM8607_IRQ_HOOK] = {
  399. .reg = PM8607_INT_STATUS3,
  400. .mask_reg = PM8607_INT_MASK_3,
  401. .offs = 1 << 3,
  402. },
  403. [PM8607_IRQ_MICIN] = {
  404. .reg = PM8607_INT_STATUS3,
  405. .mask_reg = PM8607_INT_MASK_3,
  406. .offs = 1 << 4,
  407. },
  408. [PM8607_IRQ_CHG_FAIL] = {
  409. .reg = PM8607_INT_STATUS3,
  410. .mask_reg = PM8607_INT_MASK_3,
  411. .offs = 1 << 5,
  412. },
  413. [PM8607_IRQ_CHG_DONE] = {
  414. .reg = PM8607_INT_STATUS3,
  415. .mask_reg = PM8607_INT_MASK_3,
  416. .offs = 1 << 6,
  417. },
  418. [PM8607_IRQ_CHG_FAULT] = {
  419. .reg = PM8607_INT_STATUS3,
  420. .mask_reg = PM8607_INT_MASK_3,
  421. .offs = 1 << 7,
  422. },
  423. };
  424. static irqreturn_t pm860x_irq(int irq, void *data)
  425. {
  426. struct pm860x_chip *chip = data;
  427. struct pm860x_irq_data *irq_data;
  428. struct i2c_client *i2c;
  429. int read_reg = -1, value = 0;
  430. int i;
  431. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  432. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  433. irq_data = &pm860x_irqs[i];
  434. if (read_reg != irq_data->reg) {
  435. read_reg = irq_data->reg;
  436. value = pm860x_reg_read(i2c, irq_data->reg);
  437. }
  438. if (value & irq_data->enable)
  439. handle_nested_irq(chip->irq_base + i);
  440. }
  441. return IRQ_HANDLED;
  442. }
  443. static void pm860x_irq_lock(struct irq_data *data)
  444. {
  445. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  446. mutex_lock(&chip->irq_lock);
  447. }
  448. static void pm860x_irq_sync_unlock(struct irq_data *data)
  449. {
  450. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  451. struct pm860x_irq_data *irq_data;
  452. struct i2c_client *i2c;
  453. static unsigned char cached[3] = {0x0, 0x0, 0x0};
  454. unsigned char mask[3];
  455. int i;
  456. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  457. /* Load cached value. In initial, all IRQs are masked */
  458. for (i = 0; i < 3; i++)
  459. mask[i] = cached[i];
  460. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  461. irq_data = &pm860x_irqs[i];
  462. switch (irq_data->mask_reg) {
  463. case PM8607_INT_MASK_1:
  464. mask[0] &= ~irq_data->offs;
  465. mask[0] |= irq_data->enable;
  466. break;
  467. case PM8607_INT_MASK_2:
  468. mask[1] &= ~irq_data->offs;
  469. mask[1] |= irq_data->enable;
  470. break;
  471. case PM8607_INT_MASK_3:
  472. mask[2] &= ~irq_data->offs;
  473. mask[2] |= irq_data->enable;
  474. break;
  475. default:
  476. dev_err(chip->dev, "wrong IRQ\n");
  477. break;
  478. }
  479. }
  480. /* update mask into registers */
  481. for (i = 0; i < 3; i++) {
  482. if (mask[i] != cached[i]) {
  483. cached[i] = mask[i];
  484. pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
  485. }
  486. }
  487. mutex_unlock(&chip->irq_lock);
  488. }
  489. static void pm860x_irq_enable(struct irq_data *data)
  490. {
  491. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  492. pm860x_irqs[data->irq - chip->irq_base].enable
  493. = pm860x_irqs[data->irq - chip->irq_base].offs;
  494. }
  495. static void pm860x_irq_disable(struct irq_data *data)
  496. {
  497. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  498. pm860x_irqs[data->irq - chip->irq_base].enable = 0;
  499. }
  500. static struct irq_chip pm860x_irq_chip = {
  501. .name = "88pm860x",
  502. .irq_bus_lock = pm860x_irq_lock,
  503. .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
  504. .irq_enable = pm860x_irq_enable,
  505. .irq_disable = pm860x_irq_disable,
  506. };
  507. static int __devinit device_gpadc_init(struct pm860x_chip *chip,
  508. struct pm860x_platform_data *pdata)
  509. {
  510. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  511. : chip->companion;
  512. int data;
  513. int ret;
  514. /* initialize GPADC without activating it */
  515. if (!pdata || !pdata->touch)
  516. return -EINVAL;
  517. /* set GPADC MISC1 register */
  518. data = 0;
  519. data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
  520. data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  521. data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  522. data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
  523. if (data) {
  524. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  525. if (ret < 0)
  526. goto out;
  527. }
  528. /* set tsi prebias time */
  529. if (pdata->touch->tsi_prebias) {
  530. data = pdata->touch->tsi_prebias;
  531. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  532. if (ret < 0)
  533. goto out;
  534. }
  535. /* set prebias & prechg time of pen detect */
  536. data = 0;
  537. data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
  538. data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
  539. if (data) {
  540. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  541. if (ret < 0)
  542. goto out;
  543. }
  544. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
  545. PM8607_GPADC_EN, PM8607_GPADC_EN);
  546. out:
  547. return ret;
  548. }
  549. static int __devinit device_irq_init(struct pm860x_chip *chip,
  550. struct pm860x_platform_data *pdata)
  551. {
  552. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  553. : chip->companion;
  554. unsigned char status_buf[INT_STATUS_NUM];
  555. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  556. int i, data, mask, ret = -EINVAL;
  557. int __irq;
  558. if (!pdata || !pdata->irq_base) {
  559. dev_warn(chip->dev, "No interrupt support on IRQ base\n");
  560. return -EINVAL;
  561. }
  562. mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
  563. | PM8607_B0_MISC1_INT_MASK;
  564. data = 0;
  565. chip->irq_mode = 0;
  566. if (pdata && pdata->irq_mode) {
  567. /*
  568. * irq_mode defines the way of clearing interrupt. If it's 1,
  569. * clear IRQ by write. Otherwise, clear it by read.
  570. * This control bit is valid from 88PM8607 B0 steping.
  571. */
  572. data |= PM8607_B0_MISC1_INT_CLEAR;
  573. chip->irq_mode = 1;
  574. }
  575. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
  576. if (ret < 0)
  577. goto out;
  578. /* mask all IRQs */
  579. memset(status_buf, 0, INT_STATUS_NUM);
  580. ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
  581. INT_STATUS_NUM, status_buf);
  582. if (ret < 0)
  583. goto out;
  584. if (chip->irq_mode) {
  585. /* clear interrupt status by write */
  586. memset(status_buf, 0xFF, INT_STATUS_NUM);
  587. ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
  588. INT_STATUS_NUM, status_buf);
  589. } else {
  590. /* clear interrupt status by read */
  591. ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
  592. INT_STATUS_NUM, status_buf);
  593. }
  594. if (ret < 0)
  595. goto out;
  596. mutex_init(&chip->irq_lock);
  597. chip->irq_base = pdata->irq_base;
  598. chip->core_irq = i2c->irq;
  599. if (!chip->core_irq)
  600. goto out;
  601. /* register IRQ by genirq */
  602. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  603. __irq = i + chip->irq_base;
  604. irq_set_chip_data(__irq, chip);
  605. irq_set_chip_and_handler(__irq, &pm860x_irq_chip,
  606. handle_edge_irq);
  607. irq_set_nested_thread(__irq, 1);
  608. #ifdef CONFIG_ARM
  609. set_irq_flags(__irq, IRQF_VALID);
  610. #else
  611. irq_set_noprobe(__irq);
  612. #endif
  613. }
  614. ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags,
  615. "88pm860x", chip);
  616. if (ret) {
  617. dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
  618. chip->core_irq = 0;
  619. }
  620. return 0;
  621. out:
  622. chip->core_irq = 0;
  623. return ret;
  624. }
  625. static void device_irq_exit(struct pm860x_chip *chip)
  626. {
  627. if (chip->core_irq)
  628. free_irq(chip->core_irq, chip);
  629. }
  630. int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
  631. {
  632. int ret = -EIO;
  633. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  634. chip->client : chip->companion;
  635. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  636. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  637. __func__, chip->osc_vote,
  638. chip->osc_status);
  639. mutex_lock(&chip->osc_lock);
  640. /* Update voting status */
  641. chip->osc_vote |= client;
  642. /* If reference group is off - turn on*/
  643. if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
  644. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  645. /* Enable Reference group Vsys */
  646. if (pm860x_set_bits(i2c, PM8606_VSYS,
  647. PM8606_VSYS_EN, PM8606_VSYS_EN))
  648. goto out;
  649. /*Enable Internal Oscillator */
  650. if (pm860x_set_bits(i2c, PM8606_MISC,
  651. PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
  652. goto out;
  653. /* Update status (only if writes succeed) */
  654. chip->osc_status = PM8606_REF_GP_OSC_ON;
  655. }
  656. mutex_unlock(&chip->osc_lock);
  657. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  658. __func__, chip->osc_vote,
  659. chip->osc_status, ret);
  660. return 0;
  661. out:
  662. mutex_unlock(&chip->osc_lock);
  663. return ret;
  664. }
  665. EXPORT_SYMBOL(pm8606_osc_enable);
  666. int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
  667. {
  668. int ret = -EIO;
  669. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  670. chip->client : chip->companion;
  671. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  672. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  673. __func__, chip->osc_vote,
  674. chip->osc_status);
  675. mutex_lock(&chip->osc_lock);
  676. /*Update voting status */
  677. chip->osc_vote &= ~(client);
  678. /* If reference group is off and this is the last client to release
  679. * - turn off */
  680. if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
  681. (chip->osc_vote == REF_GP_NO_CLIENTS)) {
  682. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  683. /* Disable Reference group Vsys */
  684. if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
  685. goto out;
  686. /* Disable Internal Oscillator */
  687. if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
  688. goto out;
  689. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  690. }
  691. mutex_unlock(&chip->osc_lock);
  692. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  693. __func__, chip->osc_vote,
  694. chip->osc_status, ret);
  695. return 0;
  696. out:
  697. mutex_unlock(&chip->osc_lock);
  698. return ret;
  699. }
  700. EXPORT_SYMBOL(pm8606_osc_disable);
  701. static void __devinit device_osc_init(struct i2c_client *i2c)
  702. {
  703. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  704. mutex_init(&chip->osc_lock);
  705. /* init portofino reference group voting and status */
  706. /* Disable Reference group Vsys */
  707. pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
  708. /* Disable Internal Oscillator */
  709. pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
  710. chip->osc_vote = REF_GP_NO_CLIENTS;
  711. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  712. }
  713. static void __devinit device_bk_init(struct pm860x_chip *chip,
  714. struct pm860x_platform_data *pdata)
  715. {
  716. int ret, i;
  717. if (pdata && pdata->backlight) {
  718. if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
  719. pdata->num_backlights = ARRAY_SIZE(bk_devs);
  720. for (i = 0; i < pdata->num_backlights; i++) {
  721. bk_devs[i].platform_data = &pdata->backlight[i];
  722. bk_devs[i].pdata_size =
  723. sizeof(struct pm860x_backlight_pdata);
  724. }
  725. }
  726. ret = mfd_add_devices(chip->dev, 0, bk_devs,
  727. ARRAY_SIZE(bk_devs), NULL, 0);
  728. if (ret < 0)
  729. dev_err(chip->dev, "Failed to add backlight subdev\n");
  730. }
  731. static void __devinit device_led_init(struct pm860x_chip *chip,
  732. struct pm860x_platform_data *pdata)
  733. {
  734. int ret, i;
  735. if (pdata && pdata->led) {
  736. if (pdata->num_leds > ARRAY_SIZE(led_devs))
  737. pdata->num_leds = ARRAY_SIZE(led_devs);
  738. for (i = 0; i < pdata->num_leds; i++) {
  739. led_devs[i].platform_data = &pdata->led[i];
  740. led_devs[i].pdata_size =
  741. sizeof(struct pm860x_led_pdata);
  742. }
  743. }
  744. ret = mfd_add_devices(chip->dev, 0, led_devs,
  745. ARRAY_SIZE(led_devs), NULL, 0);
  746. if (ret < 0) {
  747. dev_err(chip->dev, "Failed to add led subdev\n");
  748. return;
  749. }
  750. }
  751. static void __devinit device_regulator_init(struct pm860x_chip *chip,
  752. struct pm860x_platform_data *pdata)
  753. {
  754. int ret;
  755. if (pdata == NULL)
  756. return;
  757. if (pdata->buck1) {
  758. reg_devs[0].platform_data = pdata->buck1;
  759. reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
  760. }
  761. if (pdata->buck2) {
  762. reg_devs[1].platform_data = pdata->buck2;
  763. reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
  764. }
  765. if (pdata->buck3) {
  766. reg_devs[2].platform_data = pdata->buck3;
  767. reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
  768. }
  769. if (pdata->ldo1) {
  770. reg_devs[3].platform_data = pdata->ldo1;
  771. reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
  772. }
  773. if (pdata->ldo2) {
  774. reg_devs[4].platform_data = pdata->ldo2;
  775. reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
  776. }
  777. if (pdata->ldo3) {
  778. reg_devs[5].platform_data = pdata->ldo3;
  779. reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
  780. }
  781. if (pdata->ldo4) {
  782. reg_devs[6].platform_data = pdata->ldo4;
  783. reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
  784. }
  785. if (pdata->ldo5) {
  786. reg_devs[7].platform_data = pdata->ldo5;
  787. reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
  788. }
  789. if (pdata->ldo6) {
  790. reg_devs[8].platform_data = pdata->ldo6;
  791. reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
  792. }
  793. if (pdata->ldo7) {
  794. reg_devs[9].platform_data = pdata->ldo7;
  795. reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
  796. }
  797. if (pdata->ldo8) {
  798. reg_devs[10].platform_data = pdata->ldo8;
  799. reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
  800. }
  801. if (pdata->ldo9) {
  802. reg_devs[11].platform_data = pdata->ldo9;
  803. reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
  804. }
  805. if (pdata->ldo10) {
  806. reg_devs[12].platform_data = pdata->ldo10;
  807. reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
  808. }
  809. if (pdata->ldo12) {
  810. reg_devs[13].platform_data = pdata->ldo12;
  811. reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
  812. }
  813. if (pdata->ldo_vibrator) {
  814. reg_devs[14].platform_data = pdata->ldo_vibrator;
  815. reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
  816. }
  817. if (pdata->ldo14) {
  818. reg_devs[15].platform_data = pdata->ldo14;
  819. reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
  820. }
  821. ret = mfd_add_devices(chip->dev, 0, reg_devs,
  822. ARRAY_SIZE(reg_devs), NULL, 0);
  823. if (ret < 0) {
  824. dev_err(chip->dev, "Failed to add regulator subdev\n");
  825. return;
  826. }
  827. }
  828. static void __devinit device_rtc_init(struct pm860x_chip *chip,
  829. struct pm860x_platform_data *pdata)
  830. {
  831. int ret;
  832. if ((pdata == NULL))
  833. return;
  834. rtc_devs[0].platform_data = pdata->rtc;
  835. rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
  836. rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
  837. rtc_devs[0].resources = &rtc_resources[0];
  838. ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
  839. ARRAY_SIZE(rtc_devs), &rtc_resources[0],
  840. chip->irq_base);
  841. if (ret < 0)
  842. dev_err(chip->dev, "Failed to add rtc subdev\n");
  843. }
  844. static void __devinit device_touch_init(struct pm860x_chip *chip,
  845. struct pm860x_platform_data *pdata)
  846. {
  847. int ret;
  848. if (pdata == NULL)
  849. return;
  850. touch_devs[0].platform_data = pdata->touch;
  851. touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
  852. touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
  853. touch_devs[0].resources = &touch_resources[0];
  854. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  855. ARRAY_SIZE(touch_devs), &touch_resources[0],
  856. chip->irq_base);
  857. if (ret < 0)
  858. dev_err(chip->dev, "Failed to add touch subdev\n");
  859. }
  860. static void __devinit device_power_init(struct pm860x_chip *chip,
  861. struct pm860x_platform_data *pdata)
  862. {
  863. int ret;
  864. if (pdata == NULL)
  865. return;
  866. power_devs[0].platform_data = pdata->power;
  867. power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
  868. power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
  869. power_devs[0].resources = &battery_resources[0],
  870. ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
  871. &battery_resources[0], chip->irq_base);
  872. if (ret < 0)
  873. dev_err(chip->dev, "Failed to add battery subdev\n");
  874. power_devs[1].platform_data = pdata->power;
  875. power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
  876. power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
  877. power_devs[1].resources = &charger_resources[0],
  878. ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
  879. &charger_resources[0], chip->irq_base);
  880. if (ret < 0)
  881. dev_err(chip->dev, "Failed to add charger subdev\n");
  882. power_devs[2].platform_data = &preg_init_data;
  883. power_devs[2].pdata_size = sizeof(struct regulator_init_data);
  884. power_devs[2].num_resources = ARRAY_SIZE(preg_resources);
  885. power_devs[2].resources = &preg_resources[0],
  886. ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
  887. &preg_resources[0], chip->irq_base);
  888. if (ret < 0)
  889. dev_err(chip->dev, "Failed to add preg subdev\n");
  890. }
  891. static void __devinit device_onkey_init(struct pm860x_chip *chip,
  892. struct pm860x_platform_data *pdata)
  893. {
  894. int ret;
  895. onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
  896. onkey_devs[0].resources = &onkey_resources[0],
  897. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  898. ARRAY_SIZE(onkey_devs), &onkey_resources[0],
  899. chip->irq_base);
  900. if (ret < 0)
  901. dev_err(chip->dev, "Failed to add onkey subdev\n");
  902. }
  903. static void __devinit device_codec_init(struct pm860x_chip *chip,
  904. struct pm860x_platform_data *pdata)
  905. {
  906. int ret;
  907. codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
  908. codec_devs[0].resources = &codec_resources[0],
  909. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  910. ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
  911. if (ret < 0)
  912. dev_err(chip->dev, "Failed to add codec subdev\n");
  913. }
  914. static void __devinit device_8607_init(struct pm860x_chip *chip,
  915. struct i2c_client *i2c,
  916. struct pm860x_platform_data *pdata)
  917. {
  918. int data, ret;
  919. ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
  920. if (ret < 0) {
  921. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  922. goto out;
  923. }
  924. switch (ret & PM8607_VERSION_MASK) {
  925. case 0x40:
  926. case 0x50:
  927. dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
  928. ret);
  929. break;
  930. default:
  931. dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
  932. "Chip ID: %02x\n", ret);
  933. goto out;
  934. }
  935. ret = pm860x_reg_read(i2c, PM8607_BUCK3);
  936. if (ret < 0) {
  937. dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
  938. goto out;
  939. }
  940. if (ret & PM8607_BUCK3_DOUBLE)
  941. chip->buck3_double = 1;
  942. ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
  943. if (ret < 0) {
  944. dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
  945. goto out;
  946. }
  947. if (pdata && (pdata->i2c_port == PI2C_PORT))
  948. data = PM8607_B0_MISC1_PI2C;
  949. else
  950. data = 0;
  951. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
  952. if (ret < 0) {
  953. dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
  954. goto out;
  955. }
  956. ret = device_gpadc_init(chip, pdata);
  957. if (ret < 0)
  958. goto out;
  959. ret = device_irq_init(chip, pdata);
  960. if (ret < 0)
  961. goto out;
  962. device_regulator_init(chip, pdata);
  963. device_rtc_init(chip, pdata);
  964. device_onkey_init(chip, pdata);
  965. device_touch_init(chip, pdata);
  966. device_power_init(chip, pdata);
  967. device_codec_init(chip, pdata);
  968. out:
  969. return;
  970. }
  971. static void __devinit device_8606_init(struct pm860x_chip *chip,
  972. struct i2c_client *i2c,
  973. struct pm860x_platform_data *pdata)
  974. {
  975. device_osc_init(i2c);
  976. device_bk_init(chip, pdata);
  977. device_led_init(chip, pdata);
  978. }
  979. int __devinit pm860x_device_init(struct pm860x_chip *chip,
  980. struct pm860x_platform_data *pdata)
  981. {
  982. chip->core_irq = 0;
  983. switch (chip->id) {
  984. case CHIP_PM8606:
  985. device_8606_init(chip, chip->client, pdata);
  986. break;
  987. case CHIP_PM8607:
  988. device_8607_init(chip, chip->client, pdata);
  989. break;
  990. }
  991. if (chip->companion) {
  992. switch (chip->id) {
  993. case CHIP_PM8607:
  994. device_8606_init(chip, chip->companion, pdata);
  995. break;
  996. case CHIP_PM8606:
  997. device_8607_init(chip, chip->companion, pdata);
  998. break;
  999. }
  1000. }
  1001. return 0;
  1002. }
  1003. void __devexit pm860x_device_exit(struct pm860x_chip *chip)
  1004. {
  1005. device_irq_exit(chip);
  1006. mfd_remove_devices(chip->dev);
  1007. }
  1008. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
  1009. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  1010. MODULE_LICENSE("GPL");