88pm860x-core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 led_resources[] __devinitdata = {
  37. {PM8606_LED1_RED, PM8606_LED1_RED, "led0-red", IORESOURCE_REG,},
  38. {PM8606_LED1_GREEN, PM8606_LED1_GREEN, "led0-green", IORESOURCE_REG,},
  39. {PM8606_LED1_BLUE, PM8606_LED1_BLUE, "led0-blue", IORESOURCE_REG,},
  40. {PM8606_LED2_RED, PM8606_LED2_RED, "led1-red", IORESOURCE_REG,},
  41. {PM8606_LED2_GREEN, PM8606_LED2_GREEN, "led1-green", IORESOURCE_REG,},
  42. {PM8606_LED2_BLUE, PM8606_LED2_BLUE, "led1-blue", IORESOURCE_REG,},
  43. };
  44. static struct resource regulator_resources[] __devinitdata = {
  45. {PM8607_ID_BUCK1, PM8607_ID_BUCK1, "buck-1", IORESOURCE_REG,},
  46. {PM8607_ID_BUCK2, PM8607_ID_BUCK2, "buck-2", IORESOURCE_REG,},
  47. {PM8607_ID_BUCK3, PM8607_ID_BUCK3, "buck-3", IORESOURCE_REG,},
  48. {PM8607_ID_LDO1, PM8607_ID_LDO1, "ldo-01", IORESOURCE_REG,},
  49. {PM8607_ID_LDO2, PM8607_ID_LDO2, "ldo-02", IORESOURCE_REG,},
  50. {PM8607_ID_LDO3, PM8607_ID_LDO3, "ldo-03", IORESOURCE_REG,},
  51. {PM8607_ID_LDO4, PM8607_ID_LDO4, "ldo-04", IORESOURCE_REG,},
  52. {PM8607_ID_LDO5, PM8607_ID_LDO5, "ldo-05", IORESOURCE_REG,},
  53. {PM8607_ID_LDO6, PM8607_ID_LDO6, "ldo-06", IORESOURCE_REG,},
  54. {PM8607_ID_LDO7, PM8607_ID_LDO7, "ldo-07", IORESOURCE_REG,},
  55. {PM8607_ID_LDO8, PM8607_ID_LDO8, "ldo-08", IORESOURCE_REG,},
  56. {PM8607_ID_LDO9, PM8607_ID_LDO9, "ldo-09", IORESOURCE_REG,},
  57. {PM8607_ID_LDO10, PM8607_ID_LDO10, "ldo-10", IORESOURCE_REG,},
  58. {PM8607_ID_LDO11, PM8607_ID_LDO11, "ldo-11", IORESOURCE_REG,},
  59. {PM8607_ID_LDO12, PM8607_ID_LDO12, "ldo-12", IORESOURCE_REG,},
  60. {PM8607_ID_LDO13, PM8607_ID_LDO13, "ldo-13", IORESOURCE_REG,},
  61. {PM8607_ID_LDO14, PM8607_ID_LDO14, "ldo-14", IORESOURCE_REG,},
  62. {PM8607_ID_LDO15, PM8607_ID_LDO15, "ldo-15", IORESOURCE_REG,},
  63. };
  64. static struct resource touch_resources[] __devinitdata = {
  65. {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
  66. };
  67. static struct resource onkey_resources[] __devinitdata = {
  68. {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
  69. };
  70. static struct resource codec_resources[] __devinitdata = {
  71. /* Headset microphone insertion or removal */
  72. {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
  73. /* Hook-switch press or release */
  74. {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
  75. /* Headset insertion or removal */
  76. {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
  77. /* Audio short */
  78. {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
  79. };
  80. static struct resource battery_resources[] __devinitdata = {
  81. {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
  82. {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
  83. };
  84. static struct resource charger_resources[] __devinitdata = {
  85. {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
  86. {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,},
  87. {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout", IORESOURCE_IRQ,},
  88. {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,},
  89. {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
  90. {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
  91. };
  92. static struct resource preg_resources[] __devinitdata = {
  93. {PM8606_ID_PREG, PM8606_ID_PREG, "preg", IORESOURCE_REG,},
  94. };
  95. static struct resource rtc_resources[] __devinitdata = {
  96. {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
  97. };
  98. static struct mfd_cell bk_devs[] = {
  99. {
  100. .name = "88pm860x-backlight",
  101. .id = 0,
  102. .num_resources = ARRAY_SIZE(bk0_resources),
  103. .resources = bk0_resources,
  104. }, {
  105. .name = "88pm860x-backlight",
  106. .id = 1,
  107. .num_resources = ARRAY_SIZE(bk1_resources),
  108. .resources = bk1_resources,
  109. }, {
  110. .name = "88pm860x-backlight",
  111. .id = 2,
  112. .num_resources = ARRAY_SIZE(bk2_resources),
  113. .resources = bk2_resources,
  114. },
  115. };
  116. static struct mfd_cell led_devs[] = {
  117. {"88pm860x-led", 0,},
  118. {"88pm860x-led", 1,},
  119. {"88pm860x-led", 2,},
  120. {"88pm860x-led", 3,},
  121. {"88pm860x-led", 4,},
  122. {"88pm860x-led", 5,},
  123. };
  124. static struct mfd_cell regulator_devs[] = {
  125. {"88pm860x-regulator", 0,},
  126. {"88pm860x-regulator", 1,},
  127. {"88pm860x-regulator", 2,},
  128. {"88pm860x-regulator", 3,},
  129. {"88pm860x-regulator", 4,},
  130. {"88pm860x-regulator", 5,},
  131. {"88pm860x-regulator", 6,},
  132. {"88pm860x-regulator", 7,},
  133. {"88pm860x-regulator", 8,},
  134. {"88pm860x-regulator", 9,},
  135. {"88pm860x-regulator", 10,},
  136. {"88pm860x-regulator", 11,},
  137. {"88pm860x-regulator", 12,},
  138. {"88pm860x-regulator", 13,},
  139. {"88pm860x-regulator", 14,},
  140. {"88pm860x-regulator", 15,},
  141. {"88pm860x-regulator", 16,},
  142. {"88pm860x-regulator", 17,},
  143. };
  144. static struct mfd_cell touch_devs[] = {
  145. {"88pm860x-touch", -1,},
  146. };
  147. static struct mfd_cell onkey_devs[] = {
  148. {"88pm860x-onkey", -1,},
  149. };
  150. static struct mfd_cell codec_devs[] = {
  151. {"88pm860x-codec", -1,},
  152. };
  153. static struct regulator_consumer_supply preg_supply[] = {
  154. REGULATOR_SUPPLY("preg", "charger-manager"),
  155. };
  156. static struct regulator_init_data preg_init_data = {
  157. .num_consumer_supplies = ARRAY_SIZE(preg_supply),
  158. .consumer_supplies = &preg_supply[0],
  159. };
  160. static struct mfd_cell power_devs[] = {
  161. {"88pm860x-battery", -1,},
  162. {"88pm860x-charger", -1,},
  163. {"88pm860x-preg", -1,},
  164. };
  165. static struct mfd_cell rtc_devs[] = {
  166. {"88pm860x-rtc", -1,},
  167. };
  168. struct pm860x_irq_data {
  169. int reg;
  170. int mask_reg;
  171. int enable; /* enable or not */
  172. int offs; /* bit offset in mask register */
  173. };
  174. static struct pm860x_irq_data pm860x_irqs[] = {
  175. [PM8607_IRQ_ONKEY] = {
  176. .reg = PM8607_INT_STATUS1,
  177. .mask_reg = PM8607_INT_MASK_1,
  178. .offs = 1 << 0,
  179. },
  180. [PM8607_IRQ_EXTON] = {
  181. .reg = PM8607_INT_STATUS1,
  182. .mask_reg = PM8607_INT_MASK_1,
  183. .offs = 1 << 1,
  184. },
  185. [PM8607_IRQ_CHG] = {
  186. .reg = PM8607_INT_STATUS1,
  187. .mask_reg = PM8607_INT_MASK_1,
  188. .offs = 1 << 2,
  189. },
  190. [PM8607_IRQ_BAT] = {
  191. .reg = PM8607_INT_STATUS1,
  192. .mask_reg = PM8607_INT_MASK_1,
  193. .offs = 1 << 3,
  194. },
  195. [PM8607_IRQ_RTC] = {
  196. .reg = PM8607_INT_STATUS1,
  197. .mask_reg = PM8607_INT_MASK_1,
  198. .offs = 1 << 4,
  199. },
  200. [PM8607_IRQ_CC] = {
  201. .reg = PM8607_INT_STATUS1,
  202. .mask_reg = PM8607_INT_MASK_1,
  203. .offs = 1 << 5,
  204. },
  205. [PM8607_IRQ_VBAT] = {
  206. .reg = PM8607_INT_STATUS2,
  207. .mask_reg = PM8607_INT_MASK_2,
  208. .offs = 1 << 0,
  209. },
  210. [PM8607_IRQ_VCHG] = {
  211. .reg = PM8607_INT_STATUS2,
  212. .mask_reg = PM8607_INT_MASK_2,
  213. .offs = 1 << 1,
  214. },
  215. [PM8607_IRQ_VSYS] = {
  216. .reg = PM8607_INT_STATUS2,
  217. .mask_reg = PM8607_INT_MASK_2,
  218. .offs = 1 << 2,
  219. },
  220. [PM8607_IRQ_TINT] = {
  221. .reg = PM8607_INT_STATUS2,
  222. .mask_reg = PM8607_INT_MASK_2,
  223. .offs = 1 << 3,
  224. },
  225. [PM8607_IRQ_GPADC0] = {
  226. .reg = PM8607_INT_STATUS2,
  227. .mask_reg = PM8607_INT_MASK_2,
  228. .offs = 1 << 4,
  229. },
  230. [PM8607_IRQ_GPADC1] = {
  231. .reg = PM8607_INT_STATUS2,
  232. .mask_reg = PM8607_INT_MASK_2,
  233. .offs = 1 << 5,
  234. },
  235. [PM8607_IRQ_GPADC2] = {
  236. .reg = PM8607_INT_STATUS2,
  237. .mask_reg = PM8607_INT_MASK_2,
  238. .offs = 1 << 6,
  239. },
  240. [PM8607_IRQ_GPADC3] = {
  241. .reg = PM8607_INT_STATUS2,
  242. .mask_reg = PM8607_INT_MASK_2,
  243. .offs = 1 << 7,
  244. },
  245. [PM8607_IRQ_AUDIO_SHORT] = {
  246. .reg = PM8607_INT_STATUS3,
  247. .mask_reg = PM8607_INT_MASK_3,
  248. .offs = 1 << 0,
  249. },
  250. [PM8607_IRQ_PEN] = {
  251. .reg = PM8607_INT_STATUS3,
  252. .mask_reg = PM8607_INT_MASK_3,
  253. .offs = 1 << 1,
  254. },
  255. [PM8607_IRQ_HEADSET] = {
  256. .reg = PM8607_INT_STATUS3,
  257. .mask_reg = PM8607_INT_MASK_3,
  258. .offs = 1 << 2,
  259. },
  260. [PM8607_IRQ_HOOK] = {
  261. .reg = PM8607_INT_STATUS3,
  262. .mask_reg = PM8607_INT_MASK_3,
  263. .offs = 1 << 3,
  264. },
  265. [PM8607_IRQ_MICIN] = {
  266. .reg = PM8607_INT_STATUS3,
  267. .mask_reg = PM8607_INT_MASK_3,
  268. .offs = 1 << 4,
  269. },
  270. [PM8607_IRQ_CHG_FAIL] = {
  271. .reg = PM8607_INT_STATUS3,
  272. .mask_reg = PM8607_INT_MASK_3,
  273. .offs = 1 << 5,
  274. },
  275. [PM8607_IRQ_CHG_DONE] = {
  276. .reg = PM8607_INT_STATUS3,
  277. .mask_reg = PM8607_INT_MASK_3,
  278. .offs = 1 << 6,
  279. },
  280. [PM8607_IRQ_CHG_FAULT] = {
  281. .reg = PM8607_INT_STATUS3,
  282. .mask_reg = PM8607_INT_MASK_3,
  283. .offs = 1 << 7,
  284. },
  285. };
  286. static irqreturn_t pm860x_irq(int irq, void *data)
  287. {
  288. struct pm860x_chip *chip = data;
  289. struct pm860x_irq_data *irq_data;
  290. struct i2c_client *i2c;
  291. int read_reg = -1, value = 0;
  292. int i;
  293. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  294. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  295. irq_data = &pm860x_irqs[i];
  296. if (read_reg != irq_data->reg) {
  297. read_reg = irq_data->reg;
  298. value = pm860x_reg_read(i2c, irq_data->reg);
  299. }
  300. if (value & irq_data->enable)
  301. handle_nested_irq(chip->irq_base + i);
  302. }
  303. return IRQ_HANDLED;
  304. }
  305. static void pm860x_irq_lock(struct irq_data *data)
  306. {
  307. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  308. mutex_lock(&chip->irq_lock);
  309. }
  310. static void pm860x_irq_sync_unlock(struct irq_data *data)
  311. {
  312. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  313. struct pm860x_irq_data *irq_data;
  314. struct i2c_client *i2c;
  315. static unsigned char cached[3] = {0x0, 0x0, 0x0};
  316. unsigned char mask[3];
  317. int i;
  318. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  319. /* Load cached value. In initial, all IRQs are masked */
  320. for (i = 0; i < 3; i++)
  321. mask[i] = cached[i];
  322. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  323. irq_data = &pm860x_irqs[i];
  324. switch (irq_data->mask_reg) {
  325. case PM8607_INT_MASK_1:
  326. mask[0] &= ~irq_data->offs;
  327. mask[0] |= irq_data->enable;
  328. break;
  329. case PM8607_INT_MASK_2:
  330. mask[1] &= ~irq_data->offs;
  331. mask[1] |= irq_data->enable;
  332. break;
  333. case PM8607_INT_MASK_3:
  334. mask[2] &= ~irq_data->offs;
  335. mask[2] |= irq_data->enable;
  336. break;
  337. default:
  338. dev_err(chip->dev, "wrong IRQ\n");
  339. break;
  340. }
  341. }
  342. /* update mask into registers */
  343. for (i = 0; i < 3; i++) {
  344. if (mask[i] != cached[i]) {
  345. cached[i] = mask[i];
  346. pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
  347. }
  348. }
  349. mutex_unlock(&chip->irq_lock);
  350. }
  351. static void pm860x_irq_enable(struct irq_data *data)
  352. {
  353. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  354. pm860x_irqs[data->irq - chip->irq_base].enable
  355. = pm860x_irqs[data->irq - chip->irq_base].offs;
  356. }
  357. static void pm860x_irq_disable(struct irq_data *data)
  358. {
  359. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  360. pm860x_irqs[data->irq - chip->irq_base].enable = 0;
  361. }
  362. static struct irq_chip pm860x_irq_chip = {
  363. .name = "88pm860x",
  364. .irq_bus_lock = pm860x_irq_lock,
  365. .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
  366. .irq_enable = pm860x_irq_enable,
  367. .irq_disable = pm860x_irq_disable,
  368. };
  369. static int __devinit device_gpadc_init(struct pm860x_chip *chip,
  370. struct pm860x_platform_data *pdata)
  371. {
  372. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  373. : chip->companion;
  374. int data;
  375. int ret;
  376. /* initialize GPADC without activating it */
  377. if (!pdata || !pdata->touch)
  378. return -EINVAL;
  379. /* set GPADC MISC1 register */
  380. data = 0;
  381. data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
  382. data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  383. data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  384. data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
  385. if (data) {
  386. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  387. if (ret < 0)
  388. goto out;
  389. }
  390. /* set tsi prebias time */
  391. if (pdata->touch->tsi_prebias) {
  392. data = pdata->touch->tsi_prebias;
  393. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  394. if (ret < 0)
  395. goto out;
  396. }
  397. /* set prebias & prechg time of pen detect */
  398. data = 0;
  399. data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
  400. data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
  401. if (data) {
  402. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  403. if (ret < 0)
  404. goto out;
  405. }
  406. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
  407. PM8607_GPADC_EN, PM8607_GPADC_EN);
  408. out:
  409. return ret;
  410. }
  411. static int __devinit device_irq_init(struct pm860x_chip *chip,
  412. struct pm860x_platform_data *pdata)
  413. {
  414. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  415. : chip->companion;
  416. unsigned char status_buf[INT_STATUS_NUM];
  417. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  418. int i, data, mask, ret = -EINVAL;
  419. int __irq;
  420. if (!pdata || !pdata->irq_base) {
  421. dev_warn(chip->dev, "No interrupt support on IRQ base\n");
  422. return -EINVAL;
  423. }
  424. mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
  425. | PM8607_B0_MISC1_INT_MASK;
  426. data = 0;
  427. chip->irq_mode = 0;
  428. if (pdata && pdata->irq_mode) {
  429. /*
  430. * irq_mode defines the way of clearing interrupt. If it's 1,
  431. * clear IRQ by write. Otherwise, clear it by read.
  432. * This control bit is valid from 88PM8607 B0 steping.
  433. */
  434. data |= PM8607_B0_MISC1_INT_CLEAR;
  435. chip->irq_mode = 1;
  436. }
  437. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
  438. if (ret < 0)
  439. goto out;
  440. /* mask all IRQs */
  441. memset(status_buf, 0, INT_STATUS_NUM);
  442. ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
  443. INT_STATUS_NUM, status_buf);
  444. if (ret < 0)
  445. goto out;
  446. if (chip->irq_mode) {
  447. /* clear interrupt status by write */
  448. memset(status_buf, 0xFF, INT_STATUS_NUM);
  449. ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
  450. INT_STATUS_NUM, status_buf);
  451. } else {
  452. /* clear interrupt status by read */
  453. ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
  454. INT_STATUS_NUM, status_buf);
  455. }
  456. if (ret < 0)
  457. goto out;
  458. mutex_init(&chip->irq_lock);
  459. chip->irq_base = pdata->irq_base;
  460. chip->core_irq = i2c->irq;
  461. if (!chip->core_irq)
  462. goto out;
  463. /* register IRQ by genirq */
  464. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  465. __irq = i + chip->irq_base;
  466. irq_set_chip_data(__irq, chip);
  467. irq_set_chip_and_handler(__irq, &pm860x_irq_chip,
  468. handle_edge_irq);
  469. irq_set_nested_thread(__irq, 1);
  470. #ifdef CONFIG_ARM
  471. set_irq_flags(__irq, IRQF_VALID);
  472. #else
  473. irq_set_noprobe(__irq);
  474. #endif
  475. }
  476. ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags,
  477. "88pm860x", chip);
  478. if (ret) {
  479. dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
  480. chip->core_irq = 0;
  481. }
  482. return 0;
  483. out:
  484. chip->core_irq = 0;
  485. return ret;
  486. }
  487. static void device_irq_exit(struct pm860x_chip *chip)
  488. {
  489. if (chip->core_irq)
  490. free_irq(chip->core_irq, chip);
  491. }
  492. int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
  493. {
  494. int ret = -EIO;
  495. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  496. chip->client : chip->companion;
  497. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  498. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  499. __func__, chip->osc_vote,
  500. chip->osc_status);
  501. mutex_lock(&chip->osc_lock);
  502. /* Update voting status */
  503. chip->osc_vote |= client;
  504. /* If reference group is off - turn on*/
  505. if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
  506. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  507. /* Enable Reference group Vsys */
  508. if (pm860x_set_bits(i2c, PM8606_VSYS,
  509. PM8606_VSYS_EN, PM8606_VSYS_EN))
  510. goto out;
  511. /*Enable Internal Oscillator */
  512. if (pm860x_set_bits(i2c, PM8606_MISC,
  513. PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
  514. goto out;
  515. /* Update status (only if writes succeed) */
  516. chip->osc_status = PM8606_REF_GP_OSC_ON;
  517. }
  518. mutex_unlock(&chip->osc_lock);
  519. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  520. __func__, chip->osc_vote,
  521. chip->osc_status, ret);
  522. return 0;
  523. out:
  524. mutex_unlock(&chip->osc_lock);
  525. return ret;
  526. }
  527. EXPORT_SYMBOL(pm8606_osc_enable);
  528. int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
  529. {
  530. int ret = -EIO;
  531. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  532. chip->client : chip->companion;
  533. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  534. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  535. __func__, chip->osc_vote,
  536. chip->osc_status);
  537. mutex_lock(&chip->osc_lock);
  538. /*Update voting status */
  539. chip->osc_vote &= ~(client);
  540. /* If reference group is off and this is the last client to release
  541. * - turn off */
  542. if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
  543. (chip->osc_vote == REF_GP_NO_CLIENTS)) {
  544. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  545. /* Disable Reference group Vsys */
  546. if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
  547. goto out;
  548. /* Disable Internal Oscillator */
  549. if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
  550. goto out;
  551. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  552. }
  553. mutex_unlock(&chip->osc_lock);
  554. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  555. __func__, chip->osc_vote,
  556. chip->osc_status, ret);
  557. return 0;
  558. out:
  559. mutex_unlock(&chip->osc_lock);
  560. return ret;
  561. }
  562. EXPORT_SYMBOL(pm8606_osc_disable);
  563. static void __devinit device_osc_init(struct i2c_client *i2c)
  564. {
  565. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  566. mutex_init(&chip->osc_lock);
  567. /* init portofino reference group voting and status */
  568. /* Disable Reference group Vsys */
  569. pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
  570. /* Disable Internal Oscillator */
  571. pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
  572. chip->osc_vote = REF_GP_NO_CLIENTS;
  573. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  574. }
  575. static void __devinit device_bk_init(struct pm860x_chip *chip,
  576. struct pm860x_platform_data *pdata)
  577. {
  578. int ret, i;
  579. if (pdata && pdata->backlight) {
  580. if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
  581. pdata->num_backlights = ARRAY_SIZE(bk_devs);
  582. for (i = 0; i < pdata->num_backlights; i++) {
  583. bk_devs[i].platform_data = &pdata->backlight[i];
  584. bk_devs[i].pdata_size =
  585. sizeof(struct pm860x_backlight_pdata);
  586. }
  587. }
  588. ret = mfd_add_devices(chip->dev, 0, bk_devs,
  589. ARRAY_SIZE(bk_devs), NULL, 0);
  590. if (ret < 0)
  591. dev_err(chip->dev, "Failed to add backlight subdev\n");
  592. }
  593. static void __devinit device_led_init(struct pm860x_chip *chip,
  594. struct pm860x_platform_data *pdata)
  595. {
  596. int ret;
  597. int i, j, id;
  598. if ((pdata == NULL) || (pdata->led == NULL))
  599. return;
  600. if (pdata->num_leds > ARRAY_SIZE(led_devs))
  601. pdata->num_leds = ARRAY_SIZE(led_devs);
  602. for (i = 0; i < pdata->num_leds; i++) {
  603. led_devs[i].platform_data = &pdata->led[i];
  604. led_devs[i].pdata_size = sizeof(struct pm860x_led_pdata);
  605. for (j = 0; j < ARRAY_SIZE(led_devs); j++) {
  606. id = led_resources[j].start;
  607. if (pdata->led[i].flags != id)
  608. continue;
  609. led_devs[i].num_resources = 1;
  610. led_devs[i].resources = &led_resources[j],
  611. ret = mfd_add_devices(chip->dev, 0,
  612. &led_devs[i], 1,
  613. &led_resources[j], 0);
  614. if (ret < 0) {
  615. dev_err(chip->dev, "Failed to add "
  616. "led subdev\n");
  617. return;
  618. }
  619. }
  620. }
  621. }
  622. static void __devinit device_regulator_init(struct pm860x_chip *chip,
  623. struct pm860x_platform_data *pdata)
  624. {
  625. struct regulator_init_data *initdata;
  626. int ret;
  627. int i, seq;
  628. if ((pdata == NULL) || (pdata->regulator == NULL))
  629. return;
  630. if (pdata->num_regulators > ARRAY_SIZE(regulator_devs))
  631. pdata->num_regulators = ARRAY_SIZE(regulator_devs);
  632. for (i = 0, seq = -1; i < pdata->num_regulators; i++) {
  633. initdata = &pdata->regulator[i];
  634. seq = *(unsigned int *)initdata->driver_data;
  635. if ((seq < 0) || (seq > PM8607_ID_RG_MAX)) {
  636. dev_err(chip->dev, "Wrong ID(%d) on regulator(%s)\n",
  637. seq, initdata->constraints.name);
  638. goto out;
  639. }
  640. regulator_devs[i].platform_data = &pdata->regulator[i];
  641. regulator_devs[i].pdata_size = sizeof(struct regulator_init_data);
  642. regulator_devs[i].num_resources = 1;
  643. regulator_devs[i].resources = &regulator_resources[seq];
  644. ret = mfd_add_devices(chip->dev, 0, &regulator_devs[i], 1,
  645. &regulator_resources[seq], 0);
  646. if (ret < 0) {
  647. dev_err(chip->dev, "Failed to add regulator subdev\n");
  648. goto out;
  649. }
  650. }
  651. out:
  652. return;
  653. }
  654. static void __devinit device_rtc_init(struct pm860x_chip *chip,
  655. struct pm860x_platform_data *pdata)
  656. {
  657. int ret;
  658. if ((pdata == NULL))
  659. return;
  660. rtc_devs[0].platform_data = pdata->rtc;
  661. rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
  662. rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
  663. rtc_devs[0].resources = &rtc_resources[0];
  664. ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
  665. ARRAY_SIZE(rtc_devs), &rtc_resources[0],
  666. chip->irq_base);
  667. if (ret < 0)
  668. dev_err(chip->dev, "Failed to add rtc subdev\n");
  669. }
  670. static void __devinit device_touch_init(struct pm860x_chip *chip,
  671. struct pm860x_platform_data *pdata)
  672. {
  673. int ret;
  674. if (pdata == NULL)
  675. return;
  676. touch_devs[0].platform_data = pdata->touch;
  677. touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
  678. touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
  679. touch_devs[0].resources = &touch_resources[0];
  680. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  681. ARRAY_SIZE(touch_devs), &touch_resources[0],
  682. chip->irq_base);
  683. if (ret < 0)
  684. dev_err(chip->dev, "Failed to add touch subdev\n");
  685. }
  686. static void __devinit device_power_init(struct pm860x_chip *chip,
  687. struct pm860x_platform_data *pdata)
  688. {
  689. int ret;
  690. if (pdata == NULL)
  691. return;
  692. power_devs[0].platform_data = pdata->power;
  693. power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
  694. power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
  695. power_devs[0].resources = &battery_resources[0],
  696. ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
  697. &battery_resources[0], chip->irq_base);
  698. if (ret < 0)
  699. dev_err(chip->dev, "Failed to add battery subdev\n");
  700. power_devs[1].platform_data = pdata->power;
  701. power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
  702. power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
  703. power_devs[1].resources = &charger_resources[0],
  704. ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
  705. &charger_resources[0], chip->irq_base);
  706. if (ret < 0)
  707. dev_err(chip->dev, "Failed to add charger subdev\n");
  708. power_devs[2].platform_data = &preg_init_data;
  709. power_devs[2].pdata_size = sizeof(struct regulator_init_data);
  710. power_devs[2].num_resources = ARRAY_SIZE(preg_resources);
  711. power_devs[2].resources = &preg_resources[0],
  712. ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
  713. &preg_resources[0], chip->irq_base);
  714. if (ret < 0)
  715. dev_err(chip->dev, "Failed to add preg subdev\n");
  716. }
  717. static void __devinit device_onkey_init(struct pm860x_chip *chip,
  718. struct pm860x_platform_data *pdata)
  719. {
  720. int ret;
  721. onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
  722. onkey_devs[0].resources = &onkey_resources[0],
  723. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  724. ARRAY_SIZE(onkey_devs), &onkey_resources[0],
  725. chip->irq_base);
  726. if (ret < 0)
  727. dev_err(chip->dev, "Failed to add onkey subdev\n");
  728. }
  729. static void __devinit device_codec_init(struct pm860x_chip *chip,
  730. struct pm860x_platform_data *pdata)
  731. {
  732. int ret;
  733. codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
  734. codec_devs[0].resources = &codec_resources[0],
  735. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  736. ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
  737. if (ret < 0)
  738. dev_err(chip->dev, "Failed to add codec subdev\n");
  739. }
  740. static void __devinit device_8607_init(struct pm860x_chip *chip,
  741. struct i2c_client *i2c,
  742. struct pm860x_platform_data *pdata)
  743. {
  744. int data, ret;
  745. ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
  746. if (ret < 0) {
  747. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  748. goto out;
  749. }
  750. switch (ret & PM8607_VERSION_MASK) {
  751. case 0x40:
  752. case 0x50:
  753. dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
  754. ret);
  755. break;
  756. default:
  757. dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
  758. "Chip ID: %02x\n", ret);
  759. goto out;
  760. }
  761. ret = pm860x_reg_read(i2c, PM8607_BUCK3);
  762. if (ret < 0) {
  763. dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
  764. goto out;
  765. }
  766. if (ret & PM8607_BUCK3_DOUBLE)
  767. chip->buck3_double = 1;
  768. ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
  769. if (ret < 0) {
  770. dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
  771. goto out;
  772. }
  773. if (pdata && (pdata->i2c_port == PI2C_PORT))
  774. data = PM8607_B0_MISC1_PI2C;
  775. else
  776. data = 0;
  777. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
  778. if (ret < 0) {
  779. dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
  780. goto out;
  781. }
  782. ret = device_gpadc_init(chip, pdata);
  783. if (ret < 0)
  784. goto out;
  785. ret = device_irq_init(chip, pdata);
  786. if (ret < 0)
  787. goto out;
  788. device_regulator_init(chip, pdata);
  789. device_rtc_init(chip, pdata);
  790. device_onkey_init(chip, pdata);
  791. device_touch_init(chip, pdata);
  792. device_power_init(chip, pdata);
  793. device_codec_init(chip, pdata);
  794. out:
  795. return;
  796. }
  797. static void __devinit device_8606_init(struct pm860x_chip *chip,
  798. struct i2c_client *i2c,
  799. struct pm860x_platform_data *pdata)
  800. {
  801. device_osc_init(i2c);
  802. device_bk_init(chip, pdata);
  803. device_led_init(chip, pdata);
  804. }
  805. int __devinit pm860x_device_init(struct pm860x_chip *chip,
  806. struct pm860x_platform_data *pdata)
  807. {
  808. chip->core_irq = 0;
  809. switch (chip->id) {
  810. case CHIP_PM8606:
  811. device_8606_init(chip, chip->client, pdata);
  812. break;
  813. case CHIP_PM8607:
  814. device_8607_init(chip, chip->client, pdata);
  815. break;
  816. }
  817. if (chip->companion) {
  818. switch (chip->id) {
  819. case CHIP_PM8607:
  820. device_8606_init(chip, chip->companion, pdata);
  821. break;
  822. case CHIP_PM8606:
  823. device_8607_init(chip, chip->companion, pdata);
  824. break;
  825. }
  826. }
  827. return 0;
  828. }
  829. void __devexit pm860x_device_exit(struct pm860x_chip *chip)
  830. {
  831. device_irq_exit(chip);
  832. mfd_remove_devices(chip->dev);
  833. }
  834. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
  835. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  836. MODULE_LICENSE("GPL");