ab8500-core.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
  6. * Author: Rabin Vincent <rabin.vincent@stericsson.com>
  7. * Author: Mattias Wallin <mattias.wallin@stericsson.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/mfd/abx500.h>
  20. #include <linux/mfd/abx500/ab8500.h>
  21. #include <linux/mfd/abx500/ab8500-bm.h>
  22. #include <linux/mfd/dbx500-prcmu.h>
  23. #include <linux/regulator/ab8500.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. /*
  27. * Interrupt register offsets
  28. * Bank : 0x0E
  29. */
  30. #define AB8500_IT_SOURCE1_REG 0x00
  31. #define AB8500_IT_SOURCE2_REG 0x01
  32. #define AB8500_IT_SOURCE3_REG 0x02
  33. #define AB8500_IT_SOURCE4_REG 0x03
  34. #define AB8500_IT_SOURCE5_REG 0x04
  35. #define AB8500_IT_SOURCE6_REG 0x05
  36. #define AB8500_IT_SOURCE7_REG 0x06
  37. #define AB8500_IT_SOURCE8_REG 0x07
  38. #define AB9540_IT_SOURCE13_REG 0x0C
  39. #define AB8500_IT_SOURCE19_REG 0x12
  40. #define AB8500_IT_SOURCE20_REG 0x13
  41. #define AB8500_IT_SOURCE21_REG 0x14
  42. #define AB8500_IT_SOURCE22_REG 0x15
  43. #define AB8500_IT_SOURCE23_REG 0x16
  44. #define AB8500_IT_SOURCE24_REG 0x17
  45. /*
  46. * latch registers
  47. */
  48. #define AB8500_IT_LATCH1_REG 0x20
  49. #define AB8500_IT_LATCH2_REG 0x21
  50. #define AB8500_IT_LATCH3_REG 0x22
  51. #define AB8500_IT_LATCH4_REG 0x23
  52. #define AB8500_IT_LATCH5_REG 0x24
  53. #define AB8500_IT_LATCH6_REG 0x25
  54. #define AB8500_IT_LATCH7_REG 0x26
  55. #define AB8500_IT_LATCH8_REG 0x27
  56. #define AB8500_IT_LATCH9_REG 0x28
  57. #define AB8500_IT_LATCH10_REG 0x29
  58. #define AB8500_IT_LATCH12_REG 0x2B
  59. #define AB9540_IT_LATCH13_REG 0x2C
  60. #define AB8500_IT_LATCH19_REG 0x32
  61. #define AB8500_IT_LATCH20_REG 0x33
  62. #define AB8500_IT_LATCH21_REG 0x34
  63. #define AB8500_IT_LATCH22_REG 0x35
  64. #define AB8500_IT_LATCH23_REG 0x36
  65. #define AB8500_IT_LATCH24_REG 0x37
  66. /*
  67. * mask registers
  68. */
  69. #define AB8500_IT_MASK1_REG 0x40
  70. #define AB8500_IT_MASK2_REG 0x41
  71. #define AB8500_IT_MASK3_REG 0x42
  72. #define AB8500_IT_MASK4_REG 0x43
  73. #define AB8500_IT_MASK5_REG 0x44
  74. #define AB8500_IT_MASK6_REG 0x45
  75. #define AB8500_IT_MASK7_REG 0x46
  76. #define AB8500_IT_MASK8_REG 0x47
  77. #define AB8500_IT_MASK9_REG 0x48
  78. #define AB8500_IT_MASK10_REG 0x49
  79. #define AB8500_IT_MASK11_REG 0x4A
  80. #define AB8500_IT_MASK12_REG 0x4B
  81. #define AB8500_IT_MASK13_REG 0x4C
  82. #define AB8500_IT_MASK14_REG 0x4D
  83. #define AB8500_IT_MASK15_REG 0x4E
  84. #define AB8500_IT_MASK16_REG 0x4F
  85. #define AB8500_IT_MASK17_REG 0x50
  86. #define AB8500_IT_MASK18_REG 0x51
  87. #define AB8500_IT_MASK19_REG 0x52
  88. #define AB8500_IT_MASK20_REG 0x53
  89. #define AB8500_IT_MASK21_REG 0x54
  90. #define AB8500_IT_MASK22_REG 0x55
  91. #define AB8500_IT_MASK23_REG 0x56
  92. #define AB8500_IT_MASK24_REG 0x57
  93. /*
  94. * latch hierarchy registers
  95. */
  96. #define AB8500_IT_LATCHHIER1_REG 0x60
  97. #define AB8500_IT_LATCHHIER2_REG 0x61
  98. #define AB8500_IT_LATCHHIER3_REG 0x62
  99. #define AB8500_IT_LATCHHIER_NUM 3
  100. #define AB8500_REV_REG 0x80
  101. #define AB8500_IC_NAME_REG 0x82
  102. #define AB8500_SWITCH_OFF_STATUS 0x00
  103. #define AB8500_TURN_ON_STATUS 0x00
  104. static bool no_bm; /* No battery management */
  105. module_param(no_bm, bool, S_IRUGO);
  106. #define AB9540_MODEM_CTRL2_REG 0x23
  107. #define AB9540_MODEM_CTRL2_SWDBBRSTN_BIT BIT(2)
  108. /*
  109. * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
  110. * numbers are indexed into this array with (num / 8). The interupts are
  111. * defined in linux/mfd/ab8500.h
  112. *
  113. * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
  114. * offset 0.
  115. */
  116. /* AB8500 support */
  117. static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
  118. 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
  119. };
  120. /* AB9540 support */
  121. static const int ab9540_irq_regoffset[AB9540_NUM_IRQ_REGS] = {
  122. 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21, 12, 13, 24,
  123. };
  124. static const char ab8500_version_str[][7] = {
  125. [AB8500_VERSION_AB8500] = "AB8500",
  126. [AB8500_VERSION_AB8505] = "AB8505",
  127. [AB8500_VERSION_AB9540] = "AB9540",
  128. [AB8500_VERSION_AB8540] = "AB8540",
  129. };
  130. static int ab8500_prcmu_write(struct ab8500 *ab8500, u16 addr, u8 data)
  131. {
  132. int ret;
  133. ret = prcmu_abb_write((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
  134. if (ret < 0)
  135. dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
  136. return ret;
  137. }
  138. static int ab8500_prcmu_write_masked(struct ab8500 *ab8500, u16 addr, u8 mask,
  139. u8 data)
  140. {
  141. int ret;
  142. ret = prcmu_abb_write_masked((u8)(addr >> 8), (u8)(addr & 0xFF), &data,
  143. &mask, 1);
  144. if (ret < 0)
  145. dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
  146. return ret;
  147. }
  148. static int ab8500_prcmu_read(struct ab8500 *ab8500, u16 addr)
  149. {
  150. int ret;
  151. u8 data;
  152. ret = prcmu_abb_read((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
  153. if (ret < 0) {
  154. dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
  155. return ret;
  156. }
  157. return (int)data;
  158. }
  159. static int ab8500_get_chip_id(struct device *dev)
  160. {
  161. struct ab8500 *ab8500;
  162. if (!dev)
  163. return -EINVAL;
  164. ab8500 = dev_get_drvdata(dev->parent);
  165. return ab8500 ? (int)ab8500->chip_id : -EINVAL;
  166. }
  167. static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
  168. u8 reg, u8 data)
  169. {
  170. int ret;
  171. /*
  172. * Put the u8 bank and u8 register together into a an u16.
  173. * The bank on higher 8 bits and register in lower 8 bits.
  174. * */
  175. u16 addr = ((u16)bank) << 8 | reg;
  176. dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
  177. mutex_lock(&ab8500->lock);
  178. ret = ab8500->write(ab8500, addr, data);
  179. if (ret < 0)
  180. dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
  181. addr, ret);
  182. mutex_unlock(&ab8500->lock);
  183. return ret;
  184. }
  185. static int ab8500_set_register(struct device *dev, u8 bank,
  186. u8 reg, u8 value)
  187. {
  188. int ret;
  189. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  190. atomic_inc(&ab8500->transfer_ongoing);
  191. ret = set_register_interruptible(ab8500, bank, reg, value);
  192. atomic_dec(&ab8500->transfer_ongoing);
  193. return ret;
  194. }
  195. static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
  196. u8 reg, u8 *value)
  197. {
  198. int ret;
  199. /* put the u8 bank and u8 reg together into a an u16.
  200. * bank on higher 8 bits and reg in lower */
  201. u16 addr = ((u16)bank) << 8 | reg;
  202. mutex_lock(&ab8500->lock);
  203. ret = ab8500->read(ab8500, addr);
  204. if (ret < 0)
  205. dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
  206. addr, ret);
  207. else
  208. *value = ret;
  209. mutex_unlock(&ab8500->lock);
  210. dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
  211. return ret;
  212. }
  213. static int ab8500_get_register(struct device *dev, u8 bank,
  214. u8 reg, u8 *value)
  215. {
  216. int ret;
  217. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  218. atomic_inc(&ab8500->transfer_ongoing);
  219. ret = get_register_interruptible(ab8500, bank, reg, value);
  220. atomic_dec(&ab8500->transfer_ongoing);
  221. return ret;
  222. }
  223. static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
  224. u8 reg, u8 bitmask, u8 bitvalues)
  225. {
  226. int ret;
  227. /* put the u8 bank and u8 reg together into a an u16.
  228. * bank on higher 8 bits and reg in lower */
  229. u16 addr = ((u16)bank) << 8 | reg;
  230. mutex_lock(&ab8500->lock);
  231. if (ab8500->write_masked == NULL) {
  232. u8 data;
  233. ret = ab8500->read(ab8500, addr);
  234. if (ret < 0) {
  235. dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
  236. addr, ret);
  237. goto out;
  238. }
  239. data = (u8)ret;
  240. data = (~bitmask & data) | (bitmask & bitvalues);
  241. ret = ab8500->write(ab8500, addr, data);
  242. if (ret < 0)
  243. dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
  244. addr, ret);
  245. dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr,
  246. data);
  247. goto out;
  248. }
  249. ret = ab8500->write_masked(ab8500, addr, bitmask, bitvalues);
  250. if (ret < 0)
  251. dev_err(ab8500->dev, "failed to modify reg %#x: %d\n", addr,
  252. ret);
  253. out:
  254. mutex_unlock(&ab8500->lock);
  255. return ret;
  256. }
  257. static int ab8500_mask_and_set_register(struct device *dev,
  258. u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
  259. {
  260. int ret;
  261. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  262. atomic_inc(&ab8500->transfer_ongoing);
  263. ret= mask_and_set_register_interruptible(ab8500, bank, reg,
  264. bitmask, bitvalues);
  265. atomic_dec(&ab8500->transfer_ongoing);
  266. return ret;
  267. }
  268. static struct abx500_ops ab8500_ops = {
  269. .get_chip_id = ab8500_get_chip_id,
  270. .get_register = ab8500_get_register,
  271. .set_register = ab8500_set_register,
  272. .get_register_page = NULL,
  273. .set_register_page = NULL,
  274. .mask_and_set_register = ab8500_mask_and_set_register,
  275. .event_registers_startup_state_get = NULL,
  276. .startup_irq_enabled = NULL,
  277. .dump_all_banks = ab8500_dump_all_banks,
  278. };
  279. static void ab8500_irq_lock(struct irq_data *data)
  280. {
  281. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  282. mutex_lock(&ab8500->irq_lock);
  283. atomic_inc(&ab8500->transfer_ongoing);
  284. }
  285. static void ab8500_irq_sync_unlock(struct irq_data *data)
  286. {
  287. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  288. int i;
  289. for (i = 0; i < ab8500->mask_size; i++) {
  290. u8 old = ab8500->oldmask[i];
  291. u8 new = ab8500->mask[i];
  292. int reg;
  293. if (new == old)
  294. continue;
  295. /*
  296. * Interrupt register 12 doesn't exist prior to AB8500 version
  297. * 2.0
  298. */
  299. if (ab8500->irq_reg_offset[i] == 11 &&
  300. is_ab8500_1p1_or_earlier(ab8500))
  301. continue;
  302. ab8500->oldmask[i] = new;
  303. reg = AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i];
  304. set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
  305. }
  306. atomic_dec(&ab8500->transfer_ongoing);
  307. mutex_unlock(&ab8500->irq_lock);
  308. }
  309. static void ab8500_irq_mask(struct irq_data *data)
  310. {
  311. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  312. int offset = data->hwirq;
  313. int index = offset / 8;
  314. int mask = 1 << (offset % 8);
  315. ab8500->mask[index] |= mask;
  316. /* The AB8500 GPIOs have two interrupts each (rising & falling). */
  317. if (offset >= AB8500_INT_GPIO6R && offset <= AB8500_INT_GPIO41R)
  318. ab8500->mask[index + 2] |= mask;
  319. if (offset >= AB9540_INT_GPIO50R && offset <= AB9540_INT_GPIO54R)
  320. ab8500->mask[index + 1] |= mask;
  321. if (offset == AB8540_INT_GPIO43R || offset == AB8540_INT_GPIO44R)
  322. /* Here the falling IRQ is one bit lower */
  323. ab8500->mask[index] |= (mask << 1);
  324. }
  325. static void ab8500_irq_unmask(struct irq_data *data)
  326. {
  327. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  328. unsigned int type = irqd_get_trigger_type(data);
  329. int offset = data->hwirq;
  330. int index = offset / 8;
  331. int mask = 1 << (offset % 8);
  332. if (type & IRQ_TYPE_EDGE_RISING)
  333. ab8500->mask[index] &= ~mask;
  334. /* The AB8500 GPIOs have two interrupts each (rising & falling). */
  335. if (type & IRQ_TYPE_EDGE_FALLING) {
  336. if (offset >= AB8500_INT_GPIO6R && offset <= AB8500_INT_GPIO41R)
  337. ab8500->mask[index + 2] &= ~mask;
  338. else if (offset >= AB9540_INT_GPIO50R && offset <= AB9540_INT_GPIO54R)
  339. ab8500->mask[index + 1] &= ~mask;
  340. else if (offset == AB8540_INT_GPIO43R || offset == AB8540_INT_GPIO44R)
  341. /* Here the falling IRQ is one bit lower */
  342. ab8500->mask[index] &= ~(mask << 1);
  343. else
  344. ab8500->mask[index] &= ~mask;
  345. } else {
  346. /* Satisfies the case where type is not set. */
  347. ab8500->mask[index] &= ~mask;
  348. }
  349. }
  350. static int ab8500_irq_set_type(struct irq_data *data, unsigned int type)
  351. {
  352. return 0;
  353. }
  354. static struct irq_chip ab8500_irq_chip = {
  355. .name = "ab8500",
  356. .irq_bus_lock = ab8500_irq_lock,
  357. .irq_bus_sync_unlock = ab8500_irq_sync_unlock,
  358. .irq_mask = ab8500_irq_mask,
  359. .irq_disable = ab8500_irq_mask,
  360. .irq_unmask = ab8500_irq_unmask,
  361. .irq_set_type = ab8500_irq_set_type,
  362. };
  363. static int ab8500_handle_hierarchical_line(struct ab8500 *ab8500,
  364. int latch_offset, u8 latch_val)
  365. {
  366. int int_bit = __ffs(latch_val);
  367. int line, i;
  368. do {
  369. int_bit = __ffs(latch_val);
  370. for (i = 0; i < ab8500->mask_size; i++)
  371. if (ab8500->irq_reg_offset[i] == latch_offset)
  372. break;
  373. if (i >= ab8500->mask_size) {
  374. dev_err(ab8500->dev, "Register offset 0x%2x not declared\n",
  375. latch_offset);
  376. return -ENXIO;
  377. }
  378. line = (i << 3) + int_bit;
  379. latch_val &= ~(1 << int_bit);
  380. /*
  381. * This handles the falling edge hwirqs from the GPIO
  382. * lines. Route them back to the line registered for the
  383. * rising IRQ, as this is merely a flag for the same IRQ
  384. * in linux terms.
  385. */
  386. if (line >= AB8500_INT_GPIO6F && line <= AB8500_INT_GPIO41F)
  387. line -= 16;
  388. if (line >= AB9540_INT_GPIO50F && line <= AB9540_INT_GPIO54F)
  389. line -= 8;
  390. if (line == AB8540_INT_GPIO43F || line == AB8540_INT_GPIO44F)
  391. line += 1;
  392. handle_nested_irq(ab8500->irq_base + line);
  393. } while (latch_val);
  394. return 0;
  395. }
  396. static int ab8500_handle_hierarchical_latch(struct ab8500 *ab8500,
  397. int hier_offset, u8 hier_val)
  398. {
  399. int latch_bit, status;
  400. u8 latch_offset, latch_val;
  401. do {
  402. latch_bit = __ffs(hier_val);
  403. latch_offset = (hier_offset << 3) + latch_bit;
  404. /* Fix inconsistent ITFromLatch25 bit mapping... */
  405. if (unlikely(latch_offset == 17))
  406. latch_offset = 24;
  407. status = get_register_interruptible(ab8500,
  408. AB8500_INTERRUPT,
  409. AB8500_IT_LATCH1_REG + latch_offset,
  410. &latch_val);
  411. if (status < 0 || latch_val == 0)
  412. goto discard;
  413. status = ab8500_handle_hierarchical_line(ab8500,
  414. latch_offset, latch_val);
  415. if (status < 0)
  416. return status;
  417. discard:
  418. hier_val &= ~(1 << latch_bit);
  419. } while (hier_val);
  420. return 0;
  421. }
  422. static irqreturn_t ab8500_hierarchical_irq(int irq, void *dev)
  423. {
  424. struct ab8500 *ab8500 = dev;
  425. u8 i;
  426. dev_vdbg(ab8500->dev, "interrupt\n");
  427. /* Hierarchical interrupt version */
  428. for (i = 0; i < AB8500_IT_LATCHHIER_NUM; i++) {
  429. int status;
  430. u8 hier_val;
  431. status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
  432. AB8500_IT_LATCHHIER1_REG + i, &hier_val);
  433. if (status < 0 || hier_val == 0)
  434. continue;
  435. status = ab8500_handle_hierarchical_latch(ab8500, i, hier_val);
  436. if (status < 0)
  437. break;
  438. }
  439. return IRQ_HANDLED;
  440. }
  441. /**
  442. * ab8500_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
  443. *
  444. * @ab8500: ab8500_irq controller to operate on.
  445. * @irq: index of the interrupt requested in the chip IRQs
  446. *
  447. * Useful for drivers to request their own IRQs.
  448. */
  449. static int ab8500_irq_get_virq(struct ab8500 *ab8500, int irq)
  450. {
  451. if (!ab8500)
  452. return -EINVAL;
  453. return irq_create_mapping(ab8500->domain, irq);
  454. }
  455. static irqreturn_t ab8500_irq(int irq, void *dev)
  456. {
  457. struct ab8500 *ab8500 = dev;
  458. int i;
  459. dev_vdbg(ab8500->dev, "interrupt\n");
  460. atomic_inc(&ab8500->transfer_ongoing);
  461. for (i = 0; i < ab8500->mask_size; i++) {
  462. int regoffset = ab8500->irq_reg_offset[i];
  463. int status;
  464. u8 value;
  465. /*
  466. * Interrupt register 12 doesn't exist prior to AB8500 version
  467. * 2.0
  468. */
  469. if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
  470. continue;
  471. status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
  472. AB8500_IT_LATCH1_REG + regoffset, &value);
  473. if (status < 0 || value == 0)
  474. continue;
  475. do {
  476. int bit = __ffs(value);
  477. int line = i * 8 + bit;
  478. int virq = ab8500_irq_get_virq(ab8500, line);
  479. handle_nested_irq(virq);
  480. ab8500_debug_register_interrupt(line);
  481. value &= ~(1 << bit);
  482. } while (value);
  483. }
  484. atomic_dec(&ab8500->transfer_ongoing);
  485. return IRQ_HANDLED;
  486. }
  487. static int ab8500_irq_map(struct irq_domain *d, unsigned int virq,
  488. irq_hw_number_t hwirq)
  489. {
  490. struct ab8500 *ab8500 = d->host_data;
  491. if (!ab8500)
  492. return -EINVAL;
  493. irq_set_chip_data(virq, ab8500);
  494. irq_set_chip_and_handler(virq, &ab8500_irq_chip,
  495. handle_simple_irq);
  496. irq_set_nested_thread(virq, 1);
  497. #ifdef CONFIG_ARM
  498. set_irq_flags(virq, IRQF_VALID);
  499. #else
  500. irq_set_noprobe(virq);
  501. #endif
  502. return 0;
  503. }
  504. static struct irq_domain_ops ab8500_irq_ops = {
  505. .map = ab8500_irq_map,
  506. .xlate = irq_domain_xlate_twocell,
  507. };
  508. static int ab8500_irq_init(struct ab8500 *ab8500, struct device_node *np)
  509. {
  510. int num_irqs;
  511. if (is_ab9540(ab8500))
  512. num_irqs = AB9540_NR_IRQS;
  513. else if (is_ab8505(ab8500))
  514. num_irqs = AB8505_NR_IRQS;
  515. else
  516. num_irqs = AB8500_NR_IRQS;
  517. /* If ->irq_base is zero this will give a linear mapping */
  518. ab8500->domain = irq_domain_add_simple(NULL,
  519. num_irqs, ab8500->irq_base,
  520. &ab8500_irq_ops, ab8500);
  521. if (!ab8500->domain) {
  522. dev_err(ab8500->dev, "Failed to create irqdomain\n");
  523. return -ENOSYS;
  524. }
  525. return 0;
  526. }
  527. int ab8500_suspend(struct ab8500 *ab8500)
  528. {
  529. if (atomic_read(&ab8500->transfer_ongoing))
  530. return -EINVAL;
  531. else
  532. return 0;
  533. }
  534. static struct resource ab8500_gpadc_resources[] = {
  535. {
  536. .name = "HW_CONV_END",
  537. .start = AB8500_INT_GP_HW_ADC_CONV_END,
  538. .end = AB8500_INT_GP_HW_ADC_CONV_END,
  539. .flags = IORESOURCE_IRQ,
  540. },
  541. {
  542. .name = "SW_CONV_END",
  543. .start = AB8500_INT_GP_SW_ADC_CONV_END,
  544. .end = AB8500_INT_GP_SW_ADC_CONV_END,
  545. .flags = IORESOURCE_IRQ,
  546. },
  547. };
  548. static struct resource ab8500_rtc_resources[] = {
  549. {
  550. .name = "60S",
  551. .start = AB8500_INT_RTC_60S,
  552. .end = AB8500_INT_RTC_60S,
  553. .flags = IORESOURCE_IRQ,
  554. },
  555. {
  556. .name = "ALARM",
  557. .start = AB8500_INT_RTC_ALARM,
  558. .end = AB8500_INT_RTC_ALARM,
  559. .flags = IORESOURCE_IRQ,
  560. },
  561. };
  562. static struct resource ab8500_poweronkey_db_resources[] = {
  563. {
  564. .name = "ONKEY_DBF",
  565. .start = AB8500_INT_PON_KEY1DB_F,
  566. .end = AB8500_INT_PON_KEY1DB_F,
  567. .flags = IORESOURCE_IRQ,
  568. },
  569. {
  570. .name = "ONKEY_DBR",
  571. .start = AB8500_INT_PON_KEY1DB_R,
  572. .end = AB8500_INT_PON_KEY1DB_R,
  573. .flags = IORESOURCE_IRQ,
  574. },
  575. };
  576. static struct resource ab8500_av_acc_detect_resources[] = {
  577. {
  578. .name = "ACC_DETECT_1DB_F",
  579. .start = AB8500_INT_ACC_DETECT_1DB_F,
  580. .end = AB8500_INT_ACC_DETECT_1DB_F,
  581. .flags = IORESOURCE_IRQ,
  582. },
  583. {
  584. .name = "ACC_DETECT_1DB_R",
  585. .start = AB8500_INT_ACC_DETECT_1DB_R,
  586. .end = AB8500_INT_ACC_DETECT_1DB_R,
  587. .flags = IORESOURCE_IRQ,
  588. },
  589. {
  590. .name = "ACC_DETECT_21DB_F",
  591. .start = AB8500_INT_ACC_DETECT_21DB_F,
  592. .end = AB8500_INT_ACC_DETECT_21DB_F,
  593. .flags = IORESOURCE_IRQ,
  594. },
  595. {
  596. .name = "ACC_DETECT_21DB_R",
  597. .start = AB8500_INT_ACC_DETECT_21DB_R,
  598. .end = AB8500_INT_ACC_DETECT_21DB_R,
  599. .flags = IORESOURCE_IRQ,
  600. },
  601. {
  602. .name = "ACC_DETECT_22DB_F",
  603. .start = AB8500_INT_ACC_DETECT_22DB_F,
  604. .end = AB8500_INT_ACC_DETECT_22DB_F,
  605. .flags = IORESOURCE_IRQ,
  606. },
  607. {
  608. .name = "ACC_DETECT_22DB_R",
  609. .start = AB8500_INT_ACC_DETECT_22DB_R,
  610. .end = AB8500_INT_ACC_DETECT_22DB_R,
  611. .flags = IORESOURCE_IRQ,
  612. },
  613. };
  614. static struct resource ab8500_charger_resources[] = {
  615. {
  616. .name = "MAIN_CH_UNPLUG_DET",
  617. .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
  618. .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
  619. .flags = IORESOURCE_IRQ,
  620. },
  621. {
  622. .name = "MAIN_CHARGE_PLUG_DET",
  623. .start = AB8500_INT_MAIN_CH_PLUG_DET,
  624. .end = AB8500_INT_MAIN_CH_PLUG_DET,
  625. .flags = IORESOURCE_IRQ,
  626. },
  627. {
  628. .name = "VBUS_DET_R",
  629. .start = AB8500_INT_VBUS_DET_R,
  630. .end = AB8500_INT_VBUS_DET_R,
  631. .flags = IORESOURCE_IRQ,
  632. },
  633. {
  634. .name = "VBUS_DET_F",
  635. .start = AB8500_INT_VBUS_DET_F,
  636. .end = AB8500_INT_VBUS_DET_F,
  637. .flags = IORESOURCE_IRQ,
  638. },
  639. {
  640. .name = "USB_LINK_STATUS",
  641. .start = AB8500_INT_USB_LINK_STATUS,
  642. .end = AB8500_INT_USB_LINK_STATUS,
  643. .flags = IORESOURCE_IRQ,
  644. },
  645. {
  646. .name = "VBUS_OVV",
  647. .start = AB8500_INT_VBUS_OVV,
  648. .end = AB8500_INT_VBUS_OVV,
  649. .flags = IORESOURCE_IRQ,
  650. },
  651. {
  652. .name = "USB_CH_TH_PROT_R",
  653. .start = AB8500_INT_USB_CH_TH_PROT_R,
  654. .end = AB8500_INT_USB_CH_TH_PROT_R,
  655. .flags = IORESOURCE_IRQ,
  656. },
  657. {
  658. .name = "USB_CH_TH_PROT_F",
  659. .start = AB8500_INT_USB_CH_TH_PROT_F,
  660. .end = AB8500_INT_USB_CH_TH_PROT_F,
  661. .flags = IORESOURCE_IRQ,
  662. },
  663. {
  664. .name = "MAIN_EXT_CH_NOT_OK",
  665. .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  666. .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  667. .flags = IORESOURCE_IRQ,
  668. },
  669. {
  670. .name = "MAIN_CH_TH_PROT_R",
  671. .start = AB8500_INT_MAIN_CH_TH_PROT_R,
  672. .end = AB8500_INT_MAIN_CH_TH_PROT_R,
  673. .flags = IORESOURCE_IRQ,
  674. },
  675. {
  676. .name = "MAIN_CH_TH_PROT_F",
  677. .start = AB8500_INT_MAIN_CH_TH_PROT_F,
  678. .end = AB8500_INT_MAIN_CH_TH_PROT_F,
  679. .flags = IORESOURCE_IRQ,
  680. },
  681. {
  682. .name = "USB_CHARGER_NOT_OKR",
  683. .start = AB8500_INT_USB_CHARGER_NOT_OKR,
  684. .end = AB8500_INT_USB_CHARGER_NOT_OKR,
  685. .flags = IORESOURCE_IRQ,
  686. },
  687. {
  688. .name = "CH_WD_EXP",
  689. .start = AB8500_INT_CH_WD_EXP,
  690. .end = AB8500_INT_CH_WD_EXP,
  691. .flags = IORESOURCE_IRQ,
  692. },
  693. {
  694. .name = "VBUS_CH_DROP_END",
  695. .start = AB8500_INT_VBUS_CH_DROP_END,
  696. .end = AB8500_INT_VBUS_CH_DROP_END,
  697. .flags = IORESOURCE_IRQ,
  698. },
  699. };
  700. static struct resource ab8500_btemp_resources[] = {
  701. {
  702. .name = "BAT_CTRL_INDB",
  703. .start = AB8500_INT_BAT_CTRL_INDB,
  704. .end = AB8500_INT_BAT_CTRL_INDB,
  705. .flags = IORESOURCE_IRQ,
  706. },
  707. {
  708. .name = "BTEMP_LOW",
  709. .start = AB8500_INT_BTEMP_LOW,
  710. .end = AB8500_INT_BTEMP_LOW,
  711. .flags = IORESOURCE_IRQ,
  712. },
  713. {
  714. .name = "BTEMP_HIGH",
  715. .start = AB8500_INT_BTEMP_HIGH,
  716. .end = AB8500_INT_BTEMP_HIGH,
  717. .flags = IORESOURCE_IRQ,
  718. },
  719. {
  720. .name = "BTEMP_LOW_MEDIUM",
  721. .start = AB8500_INT_BTEMP_LOW_MEDIUM,
  722. .end = AB8500_INT_BTEMP_LOW_MEDIUM,
  723. .flags = IORESOURCE_IRQ,
  724. },
  725. {
  726. .name = "BTEMP_MEDIUM_HIGH",
  727. .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
  728. .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
  729. .flags = IORESOURCE_IRQ,
  730. },
  731. };
  732. static struct resource ab8500_fg_resources[] = {
  733. {
  734. .name = "NCONV_ACCU",
  735. .start = AB8500_INT_CCN_CONV_ACC,
  736. .end = AB8500_INT_CCN_CONV_ACC,
  737. .flags = IORESOURCE_IRQ,
  738. },
  739. {
  740. .name = "BATT_OVV",
  741. .start = AB8500_INT_BATT_OVV,
  742. .end = AB8500_INT_BATT_OVV,
  743. .flags = IORESOURCE_IRQ,
  744. },
  745. {
  746. .name = "LOW_BAT_F",
  747. .start = AB8500_INT_LOW_BAT_F,
  748. .end = AB8500_INT_LOW_BAT_F,
  749. .flags = IORESOURCE_IRQ,
  750. },
  751. {
  752. .name = "LOW_BAT_R",
  753. .start = AB8500_INT_LOW_BAT_R,
  754. .end = AB8500_INT_LOW_BAT_R,
  755. .flags = IORESOURCE_IRQ,
  756. },
  757. {
  758. .name = "CC_INT_CALIB",
  759. .start = AB8500_INT_CC_INT_CALIB,
  760. .end = AB8500_INT_CC_INT_CALIB,
  761. .flags = IORESOURCE_IRQ,
  762. },
  763. {
  764. .name = "CCEOC",
  765. .start = AB8500_INT_CCEOC,
  766. .end = AB8500_INT_CCEOC,
  767. .flags = IORESOURCE_IRQ,
  768. },
  769. };
  770. static struct resource ab8500_chargalg_resources[] = {};
  771. #ifdef CONFIG_DEBUG_FS
  772. static struct resource ab8500_debug_resources[] = {
  773. {
  774. .name = "IRQ_FIRST",
  775. .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  776. .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  777. .flags = IORESOURCE_IRQ,
  778. },
  779. {
  780. .name = "IRQ_LAST",
  781. .start = AB8500_INT_XTAL32K_KO,
  782. .end = AB8500_INT_XTAL32K_KO,
  783. .flags = IORESOURCE_IRQ,
  784. },
  785. };
  786. #endif
  787. static struct resource ab8500_usb_resources[] = {
  788. {
  789. .name = "ID_WAKEUP_R",
  790. .start = AB8500_INT_ID_WAKEUP_R,
  791. .end = AB8500_INT_ID_WAKEUP_R,
  792. .flags = IORESOURCE_IRQ,
  793. },
  794. {
  795. .name = "ID_WAKEUP_F",
  796. .start = AB8500_INT_ID_WAKEUP_F,
  797. .end = AB8500_INT_ID_WAKEUP_F,
  798. .flags = IORESOURCE_IRQ,
  799. },
  800. {
  801. .name = "VBUS_DET_F",
  802. .start = AB8500_INT_VBUS_DET_F,
  803. .end = AB8500_INT_VBUS_DET_F,
  804. .flags = IORESOURCE_IRQ,
  805. },
  806. {
  807. .name = "VBUS_DET_R",
  808. .start = AB8500_INT_VBUS_DET_R,
  809. .end = AB8500_INT_VBUS_DET_R,
  810. .flags = IORESOURCE_IRQ,
  811. },
  812. {
  813. .name = "USB_LINK_STATUS",
  814. .start = AB8500_INT_USB_LINK_STATUS,
  815. .end = AB8500_INT_USB_LINK_STATUS,
  816. .flags = IORESOURCE_IRQ,
  817. },
  818. {
  819. .name = "USB_ADP_PROBE_PLUG",
  820. .start = AB8500_INT_ADP_PROBE_PLUG,
  821. .end = AB8500_INT_ADP_PROBE_PLUG,
  822. .flags = IORESOURCE_IRQ,
  823. },
  824. {
  825. .name = "USB_ADP_PROBE_UNPLUG",
  826. .start = AB8500_INT_ADP_PROBE_UNPLUG,
  827. .end = AB8500_INT_ADP_PROBE_UNPLUG,
  828. .flags = IORESOURCE_IRQ,
  829. },
  830. };
  831. static struct resource ab8505_iddet_resources[] = {
  832. {
  833. .name = "KeyDeglitch",
  834. .start = AB8505_INT_KEYDEGLITCH,
  835. .end = AB8505_INT_KEYDEGLITCH,
  836. .flags = IORESOURCE_IRQ,
  837. },
  838. {
  839. .name = "KP",
  840. .start = AB8505_INT_KP,
  841. .end = AB8505_INT_KP,
  842. .flags = IORESOURCE_IRQ,
  843. },
  844. {
  845. .name = "IKP",
  846. .start = AB8505_INT_IKP,
  847. .end = AB8505_INT_IKP,
  848. .flags = IORESOURCE_IRQ,
  849. },
  850. {
  851. .name = "IKR",
  852. .start = AB8505_INT_IKR,
  853. .end = AB8505_INT_IKR,
  854. .flags = IORESOURCE_IRQ,
  855. },
  856. {
  857. .name = "KeyStuck",
  858. .start = AB8505_INT_KEYSTUCK,
  859. .end = AB8505_INT_KEYSTUCK,
  860. .flags = IORESOURCE_IRQ,
  861. },
  862. };
  863. static struct resource ab8500_temp_resources[] = {
  864. {
  865. .name = "ABX500_TEMP_WARM",
  866. .start = AB8500_INT_TEMP_WARM,
  867. .end = AB8500_INT_TEMP_WARM,
  868. .flags = IORESOURCE_IRQ,
  869. },
  870. };
  871. static struct mfd_cell abx500_common_devs[] = {
  872. #ifdef CONFIG_DEBUG_FS
  873. {
  874. .name = "ab8500-debug",
  875. .of_compatible = "stericsson,ab8500-debug",
  876. .num_resources = ARRAY_SIZE(ab8500_debug_resources),
  877. .resources = ab8500_debug_resources,
  878. },
  879. #endif
  880. {
  881. .name = "ab8500-sysctrl",
  882. .of_compatible = "stericsson,ab8500-sysctrl",
  883. },
  884. {
  885. .name = "ab8500-regulator",
  886. .of_compatible = "stericsson,ab8500-regulator",
  887. },
  888. {
  889. .name = "abx500-clk",
  890. .of_compatible = "stericsson,abx500-clk",
  891. },
  892. {
  893. .name = "ab8500-gpadc",
  894. .of_compatible = "stericsson,ab8500-gpadc",
  895. .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
  896. .resources = ab8500_gpadc_resources,
  897. },
  898. {
  899. .name = "ab8500-rtc",
  900. .of_compatible = "stericsson,ab8500-rtc",
  901. .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
  902. .resources = ab8500_rtc_resources,
  903. },
  904. {
  905. .name = "ab8500-acc-det",
  906. .of_compatible = "stericsson,ab8500-acc-det",
  907. .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
  908. .resources = ab8500_av_acc_detect_resources,
  909. },
  910. {
  911. .name = "ab8500-poweron-key",
  912. .of_compatible = "stericsson,ab8500-poweron-key",
  913. .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
  914. .resources = ab8500_poweronkey_db_resources,
  915. },
  916. {
  917. .name = "ab8500-pwm",
  918. .of_compatible = "stericsson,ab8500-pwm",
  919. .id = 1,
  920. },
  921. {
  922. .name = "ab8500-pwm",
  923. .of_compatible = "stericsson,ab8500-pwm",
  924. .id = 2,
  925. },
  926. {
  927. .name = "ab8500-pwm",
  928. .of_compatible = "stericsson,ab8500-pwm",
  929. .id = 3,
  930. },
  931. {
  932. .name = "ab8500-leds",
  933. .of_compatible = "stericsson,ab8500-leds",
  934. },
  935. {
  936. .name = "ab8500-denc",
  937. .of_compatible = "stericsson,ab8500-denc",
  938. },
  939. {
  940. .name = "abx500-temp",
  941. .of_compatible = "stericsson,abx500-temp",
  942. .num_resources = ARRAY_SIZE(ab8500_temp_resources),
  943. .resources = ab8500_temp_resources,
  944. },
  945. };
  946. static struct mfd_cell ab8500_bm_devs[] = {
  947. {
  948. .name = "ab8500-charger",
  949. .of_compatible = "stericsson,ab8500-charger",
  950. .num_resources = ARRAY_SIZE(ab8500_charger_resources),
  951. .resources = ab8500_charger_resources,
  952. .platform_data = &ab8500_bm_data,
  953. .pdata_size = sizeof(ab8500_bm_data),
  954. },
  955. {
  956. .name = "ab8500-btemp",
  957. .of_compatible = "stericsson,ab8500-btemp",
  958. .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
  959. .resources = ab8500_btemp_resources,
  960. .platform_data = &ab8500_bm_data,
  961. .pdata_size = sizeof(ab8500_bm_data),
  962. },
  963. {
  964. .name = "ab8500-fg",
  965. .of_compatible = "stericsson,ab8500-fg",
  966. .num_resources = ARRAY_SIZE(ab8500_fg_resources),
  967. .resources = ab8500_fg_resources,
  968. .platform_data = &ab8500_bm_data,
  969. .pdata_size = sizeof(ab8500_bm_data),
  970. },
  971. {
  972. .name = "ab8500-chargalg",
  973. .of_compatible = "stericsson,ab8500-chargalg",
  974. .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
  975. .resources = ab8500_chargalg_resources,
  976. .platform_data = &ab8500_bm_data,
  977. .pdata_size = sizeof(ab8500_bm_data),
  978. },
  979. };
  980. static struct mfd_cell ab8500_devs[] = {
  981. {
  982. .name = "pinctrl-ab8500",
  983. .of_compatible = "stericsson,ab8500-gpio",
  984. },
  985. {
  986. .name = "ab8500-usb",
  987. .of_compatible = "stericsson,ab8500-usb",
  988. .num_resources = ARRAY_SIZE(ab8500_usb_resources),
  989. .resources = ab8500_usb_resources,
  990. },
  991. {
  992. .name = "ab8500-codec",
  993. .of_compatible = "stericsson,ab8500-codec",
  994. },
  995. };
  996. static struct mfd_cell ab9540_devs[] = {
  997. {
  998. .name = "pinctrl-ab9540",
  999. .of_compatible = "stericsson,ab9540-gpio",
  1000. },
  1001. {
  1002. .name = "ab9540-usb",
  1003. .num_resources = ARRAY_SIZE(ab8500_usb_resources),
  1004. .resources = ab8500_usb_resources,
  1005. },
  1006. {
  1007. .name = "ab9540-codec",
  1008. },
  1009. };
  1010. /* Device list common to ab9540 and ab8505 */
  1011. static struct mfd_cell ab9540_ab8505_devs[] = {
  1012. {
  1013. .name = "ab-iddet",
  1014. .num_resources = ARRAY_SIZE(ab8505_iddet_resources),
  1015. .resources = ab8505_iddet_resources,
  1016. },
  1017. };
  1018. static ssize_t show_chip_id(struct device *dev,
  1019. struct device_attribute *attr, char *buf)
  1020. {
  1021. struct ab8500 *ab8500;
  1022. ab8500 = dev_get_drvdata(dev);
  1023. return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
  1024. }
  1025. /*
  1026. * ab8500 has switched off due to (SWITCH_OFF_STATUS):
  1027. * 0x01 Swoff bit programming
  1028. * 0x02 Thermal protection activation
  1029. * 0x04 Vbat lower then BattOk falling threshold
  1030. * 0x08 Watchdog expired
  1031. * 0x10 Non presence of 32kHz clock
  1032. * 0x20 Battery level lower than power on reset threshold
  1033. * 0x40 Power on key 1 pressed longer than 10 seconds
  1034. * 0x80 DB8500 thermal shutdown
  1035. */
  1036. static ssize_t show_switch_off_status(struct device *dev,
  1037. struct device_attribute *attr, char *buf)
  1038. {
  1039. int ret;
  1040. u8 value;
  1041. struct ab8500 *ab8500;
  1042. ab8500 = dev_get_drvdata(dev);
  1043. ret = get_register_interruptible(ab8500, AB8500_RTC,
  1044. AB8500_SWITCH_OFF_STATUS, &value);
  1045. if (ret < 0)
  1046. return ret;
  1047. return sprintf(buf, "%#x\n", value);
  1048. }
  1049. /*
  1050. * ab8500 has turned on due to (TURN_ON_STATUS):
  1051. * 0x01 PORnVbat
  1052. * 0x02 PonKey1dbF
  1053. * 0x04 PonKey2dbF
  1054. * 0x08 RTCAlarm
  1055. * 0x10 MainChDet
  1056. * 0x20 VbusDet
  1057. * 0x40 UsbIDDetect
  1058. * 0x80 Reserved
  1059. */
  1060. static ssize_t show_turn_on_status(struct device *dev,
  1061. struct device_attribute *attr, char *buf)
  1062. {
  1063. int ret;
  1064. u8 value;
  1065. struct ab8500 *ab8500;
  1066. ab8500 = dev_get_drvdata(dev);
  1067. ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
  1068. AB8500_TURN_ON_STATUS, &value);
  1069. if (ret < 0)
  1070. return ret;
  1071. return sprintf(buf, "%#x\n", value);
  1072. }
  1073. static ssize_t show_ab9540_dbbrstn(struct device *dev,
  1074. struct device_attribute *attr, char *buf)
  1075. {
  1076. struct ab8500 *ab8500;
  1077. int ret;
  1078. u8 value;
  1079. ab8500 = dev_get_drvdata(dev);
  1080. ret = get_register_interruptible(ab8500, AB8500_REGU_CTRL2,
  1081. AB9540_MODEM_CTRL2_REG, &value);
  1082. if (ret < 0)
  1083. return ret;
  1084. return sprintf(buf, "%d\n",
  1085. (value & AB9540_MODEM_CTRL2_SWDBBRSTN_BIT) ? 1 : 0);
  1086. }
  1087. static ssize_t store_ab9540_dbbrstn(struct device *dev,
  1088. struct device_attribute *attr, const char *buf, size_t count)
  1089. {
  1090. struct ab8500 *ab8500;
  1091. int ret = count;
  1092. int err;
  1093. u8 bitvalues;
  1094. ab8500 = dev_get_drvdata(dev);
  1095. if (count > 0) {
  1096. switch (buf[0]) {
  1097. case '0':
  1098. bitvalues = 0;
  1099. break;
  1100. case '1':
  1101. bitvalues = AB9540_MODEM_CTRL2_SWDBBRSTN_BIT;
  1102. break;
  1103. default:
  1104. goto exit;
  1105. }
  1106. err = mask_and_set_register_interruptible(ab8500,
  1107. AB8500_REGU_CTRL2, AB9540_MODEM_CTRL2_REG,
  1108. AB9540_MODEM_CTRL2_SWDBBRSTN_BIT, bitvalues);
  1109. if (err)
  1110. dev_info(ab8500->dev,
  1111. "Failed to set DBBRSTN %c, err %#x\n",
  1112. buf[0], err);
  1113. }
  1114. exit:
  1115. return ret;
  1116. }
  1117. static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
  1118. static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
  1119. static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
  1120. static DEVICE_ATTR(dbbrstn, S_IRUGO | S_IWUSR,
  1121. show_ab9540_dbbrstn, store_ab9540_dbbrstn);
  1122. static struct attribute *ab8500_sysfs_entries[] = {
  1123. &dev_attr_chip_id.attr,
  1124. &dev_attr_switch_off_status.attr,
  1125. &dev_attr_turn_on_status.attr,
  1126. NULL,
  1127. };
  1128. static struct attribute *ab9540_sysfs_entries[] = {
  1129. &dev_attr_chip_id.attr,
  1130. &dev_attr_switch_off_status.attr,
  1131. &dev_attr_turn_on_status.attr,
  1132. &dev_attr_dbbrstn.attr,
  1133. NULL,
  1134. };
  1135. static struct attribute_group ab8500_attr_group = {
  1136. .attrs = ab8500_sysfs_entries,
  1137. };
  1138. static struct attribute_group ab9540_attr_group = {
  1139. .attrs = ab9540_sysfs_entries,
  1140. };
  1141. static int ab8500_probe(struct platform_device *pdev)
  1142. {
  1143. static char *switch_off_status[] = {
  1144. "Swoff bit programming",
  1145. "Thermal protection activation",
  1146. "Vbat lower then BattOk falling threshold",
  1147. "Watchdog expired",
  1148. "Non presence of 32kHz clock",
  1149. "Battery level lower than power on reset threshold",
  1150. "Power on key 1 pressed longer than 10 seconds",
  1151. "DB8500 thermal shutdown"};
  1152. struct ab8500_platform_data *plat = dev_get_platdata(&pdev->dev);
  1153. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1154. enum ab8500_version version = AB8500_VERSION_UNDEFINED;
  1155. struct device_node *np = pdev->dev.of_node;
  1156. struct ab8500 *ab8500;
  1157. struct resource *resource;
  1158. int ret;
  1159. int i;
  1160. u8 value;
  1161. ab8500 = devm_kzalloc(&pdev->dev, sizeof *ab8500, GFP_KERNEL);
  1162. if (!ab8500)
  1163. return -ENOMEM;
  1164. if (plat)
  1165. ab8500->irq_base = plat->irq_base;
  1166. ab8500->dev = &pdev->dev;
  1167. resource = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1168. if (!resource)
  1169. return -ENODEV;
  1170. ab8500->irq = resource->start;
  1171. ab8500->read = ab8500_prcmu_read;
  1172. ab8500->write = ab8500_prcmu_write;
  1173. ab8500->write_masked = ab8500_prcmu_write_masked;
  1174. mutex_init(&ab8500->lock);
  1175. mutex_init(&ab8500->irq_lock);
  1176. atomic_set(&ab8500->transfer_ongoing, 0);
  1177. platform_set_drvdata(pdev, ab8500);
  1178. if (platid)
  1179. version = platid->driver_data;
  1180. if (version != AB8500_VERSION_UNDEFINED)
  1181. ab8500->version = version;
  1182. else {
  1183. ret = get_register_interruptible(ab8500, AB8500_MISC,
  1184. AB8500_IC_NAME_REG, &value);
  1185. if (ret < 0)
  1186. return ret;
  1187. ab8500->version = value;
  1188. }
  1189. ret = get_register_interruptible(ab8500, AB8500_MISC,
  1190. AB8500_REV_REG, &value);
  1191. if (ret < 0)
  1192. return ret;
  1193. ab8500->chip_id = value;
  1194. dev_info(ab8500->dev, "detected chip, %s rev. %1x.%1x\n",
  1195. ab8500_version_str[ab8500->version],
  1196. ab8500->chip_id >> 4,
  1197. ab8500->chip_id & 0x0F);
  1198. /* Configure AB8500 or AB9540 IRQ */
  1199. if (is_ab9540(ab8500) || is_ab8505(ab8500)) {
  1200. ab8500->mask_size = AB9540_NUM_IRQ_REGS;
  1201. ab8500->irq_reg_offset = ab9540_irq_regoffset;
  1202. } else {
  1203. ab8500->mask_size = AB8500_NUM_IRQ_REGS;
  1204. ab8500->irq_reg_offset = ab8500_irq_regoffset;
  1205. }
  1206. ab8500->mask = devm_kzalloc(&pdev->dev, ab8500->mask_size, GFP_KERNEL);
  1207. if (!ab8500->mask)
  1208. return -ENOMEM;
  1209. ab8500->oldmask = devm_kzalloc(&pdev->dev, ab8500->mask_size, GFP_KERNEL);
  1210. if (!ab8500->oldmask)
  1211. return -ENOMEM;
  1212. /*
  1213. * ab8500 has switched off due to (SWITCH_OFF_STATUS):
  1214. * 0x01 Swoff bit programming
  1215. * 0x02 Thermal protection activation
  1216. * 0x04 Vbat lower then BattOk falling threshold
  1217. * 0x08 Watchdog expired
  1218. * 0x10 Non presence of 32kHz clock
  1219. * 0x20 Battery level lower than power on reset threshold
  1220. * 0x40 Power on key 1 pressed longer than 10 seconds
  1221. * 0x80 DB8500 thermal shutdown
  1222. */
  1223. ret = get_register_interruptible(ab8500, AB8500_RTC,
  1224. AB8500_SWITCH_OFF_STATUS, &value);
  1225. if (ret < 0)
  1226. return ret;
  1227. dev_info(ab8500->dev, "switch off cause(s) (%#x): ", value);
  1228. if (value) {
  1229. for (i = 0; i < ARRAY_SIZE(switch_off_status); i++) {
  1230. if (value & 1)
  1231. printk(KERN_CONT " \"%s\"",
  1232. switch_off_status[i]);
  1233. value = value >> 1;
  1234. }
  1235. printk(KERN_CONT "\n");
  1236. } else {
  1237. printk(KERN_CONT " None\n");
  1238. }
  1239. if (plat && plat->init)
  1240. plat->init(ab8500);
  1241. /* Clear and mask all interrupts */
  1242. for (i = 0; i < ab8500->mask_size; i++) {
  1243. /*
  1244. * Interrupt register 12 doesn't exist prior to AB8500 version
  1245. * 2.0
  1246. */
  1247. if (ab8500->irq_reg_offset[i] == 11 &&
  1248. is_ab8500_1p1_or_earlier(ab8500))
  1249. continue;
  1250. get_register_interruptible(ab8500, AB8500_INTERRUPT,
  1251. AB8500_IT_LATCH1_REG + ab8500->irq_reg_offset[i],
  1252. &value);
  1253. set_register_interruptible(ab8500, AB8500_INTERRUPT,
  1254. AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i], 0xff);
  1255. }
  1256. ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
  1257. if (ret)
  1258. return ret;
  1259. for (i = 0; i < ab8500->mask_size; i++)
  1260. ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
  1261. ret = ab8500_irq_init(ab8500, np);
  1262. if (ret)
  1263. return ret;
  1264. /* Activate this feature only in ab9540 */
  1265. /* till tests are done on ab8500 1p2 or later*/
  1266. if (is_ab9540(ab8500)) {
  1267. ret = devm_request_threaded_irq(&pdev->dev, ab8500->irq, NULL,
  1268. ab8500_hierarchical_irq,
  1269. IRQF_ONESHOT | IRQF_NO_SUSPEND,
  1270. "ab8500", ab8500);
  1271. }
  1272. else {
  1273. ret = devm_request_threaded_irq(&pdev->dev, ab8500->irq, NULL,
  1274. ab8500_irq,
  1275. IRQF_ONESHOT | IRQF_NO_SUSPEND,
  1276. "ab8500", ab8500);
  1277. if (ret)
  1278. return ret;
  1279. }
  1280. ret = mfd_add_devices(ab8500->dev, 0, abx500_common_devs,
  1281. ARRAY_SIZE(abx500_common_devs), NULL,
  1282. ab8500->irq_base, ab8500->domain);
  1283. if (ret)
  1284. return ret;
  1285. if (is_ab9540(ab8500))
  1286. ret = mfd_add_devices(ab8500->dev, 0, ab9540_devs,
  1287. ARRAY_SIZE(ab9540_devs), NULL,
  1288. ab8500->irq_base, ab8500->domain);
  1289. else
  1290. ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
  1291. ARRAY_SIZE(ab8500_devs), NULL,
  1292. ab8500->irq_base, ab8500->domain);
  1293. if (ret)
  1294. return ret;
  1295. if (is_ab9540(ab8500) || is_ab8505(ab8500))
  1296. ret = mfd_add_devices(ab8500->dev, 0, ab9540_ab8505_devs,
  1297. ARRAY_SIZE(ab9540_ab8505_devs), NULL,
  1298. ab8500->irq_base, ab8500->domain);
  1299. if (ret)
  1300. return ret;
  1301. if (!no_bm) {
  1302. /* Add battery management devices */
  1303. ret = mfd_add_devices(ab8500->dev, 0, ab8500_bm_devs,
  1304. ARRAY_SIZE(ab8500_bm_devs), NULL,
  1305. ab8500->irq_base, ab8500->domain);
  1306. if (ret)
  1307. dev_err(ab8500->dev, "error adding bm devices\n");
  1308. }
  1309. if (is_ab9540(ab8500))
  1310. ret = sysfs_create_group(&ab8500->dev->kobj,
  1311. &ab9540_attr_group);
  1312. else
  1313. ret = sysfs_create_group(&ab8500->dev->kobj,
  1314. &ab8500_attr_group);
  1315. if (ret)
  1316. dev_err(ab8500->dev, "error creating sysfs entries\n");
  1317. return ret;
  1318. }
  1319. static int ab8500_remove(struct platform_device *pdev)
  1320. {
  1321. struct ab8500 *ab8500 = platform_get_drvdata(pdev);
  1322. if (is_ab9540(ab8500))
  1323. sysfs_remove_group(&ab8500->dev->kobj, &ab9540_attr_group);
  1324. else
  1325. sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
  1326. mfd_remove_devices(ab8500->dev);
  1327. return 0;
  1328. }
  1329. static const struct platform_device_id ab8500_id[] = {
  1330. { "ab8500-core", AB8500_VERSION_AB8500 },
  1331. { "ab8505-i2c", AB8500_VERSION_AB8505 },
  1332. { "ab9540-i2c", AB8500_VERSION_AB9540 },
  1333. { "ab8540-i2c", AB8500_VERSION_AB8540 },
  1334. { }
  1335. };
  1336. static struct platform_driver ab8500_core_driver = {
  1337. .driver = {
  1338. .name = "ab8500-core",
  1339. .owner = THIS_MODULE,
  1340. },
  1341. .probe = ab8500_probe,
  1342. .remove = ab8500_remove,
  1343. .id_table = ab8500_id,
  1344. };
  1345. static int __init ab8500_core_init(void)
  1346. {
  1347. return platform_driver_register(&ab8500_core_driver);
  1348. }
  1349. static void __exit ab8500_core_exit(void)
  1350. {
  1351. platform_driver_unregister(&ab8500_core_driver);
  1352. }
  1353. core_initcall(ab8500_core_init);
  1354. module_exit(ab8500_core_exit);
  1355. MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
  1356. MODULE_DESCRIPTION("AB8500 MFD core");
  1357. MODULE_LICENSE("GPL v2");