88pm860x-core.c 25 KB

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