88pm860x-core.c 26 KB

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