ab3100-core.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * Copyright (C) 2007-2010 ST-Ericsson
  3. * License terms: GNU General Public License (GPL) version 2
  4. * Low-level core for exclusive access to the AB3100 IC on the I2C bus
  5. * and some basic chip-configuration.
  6. * Author: Linus Walleij <linus.walleij@stericsson.com>
  7. */
  8. #include <linux/i2c.h>
  9. #include <linux/mutex.h>
  10. #include <linux/list.h>
  11. #include <linux/notifier.h>
  12. #include <linux/slab.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/random.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/mfd/abx500.h>
  24. /* These are the only registers inside AB3100 used in this main file */
  25. /* Interrupt event registers */
  26. #define AB3100_EVENTA1 0x21
  27. #define AB3100_EVENTA2 0x22
  28. #define AB3100_EVENTA3 0x23
  29. /* AB3100 DAC converter registers */
  30. #define AB3100_DIS 0x00
  31. #define AB3100_D0C 0x01
  32. #define AB3100_D1C 0x02
  33. #define AB3100_D2C 0x03
  34. #define AB3100_D3C 0x04
  35. /* Chip ID register */
  36. #define AB3100_CID 0x20
  37. /* AB3100 interrupt registers */
  38. #define AB3100_IMRA1 0x24
  39. #define AB3100_IMRA2 0x25
  40. #define AB3100_IMRA3 0x26
  41. #define AB3100_IMRB1 0x2B
  42. #define AB3100_IMRB2 0x2C
  43. #define AB3100_IMRB3 0x2D
  44. /* System Power Monitoring and control registers */
  45. #define AB3100_MCA 0x2E
  46. #define AB3100_MCB 0x2F
  47. /* SIM power up */
  48. #define AB3100_SUP 0x50
  49. /*
  50. * I2C communication
  51. *
  52. * The AB3100 is usually assigned address 0x48 (7-bit)
  53. * The chip is defined in the platform i2c_board_data section.
  54. */
  55. static int ab3100_get_chip_id(struct device *dev)
  56. {
  57. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  58. return (int)ab3100->chip_id;
  59. }
  60. static int ab3100_set_register_interruptible(struct ab3100 *ab3100,
  61. u8 reg, u8 regval)
  62. {
  63. u8 regandval[2] = {reg, regval};
  64. int err;
  65. err = mutex_lock_interruptible(&ab3100->access_mutex);
  66. if (err)
  67. return err;
  68. /*
  69. * A two-byte write message with the first byte containing the register
  70. * number and the second byte containing the value to be written
  71. * effectively sets a register in the AB3100.
  72. */
  73. err = i2c_master_send(ab3100->i2c_client, regandval, 2);
  74. if (err < 0) {
  75. dev_err(ab3100->dev,
  76. "write error (write register): %d\n",
  77. err);
  78. } else if (err != 2) {
  79. dev_err(ab3100->dev,
  80. "write error (write register) "
  81. "%d bytes transferred (expected 2)\n",
  82. err);
  83. err = -EIO;
  84. } else {
  85. /* All is well */
  86. err = 0;
  87. }
  88. mutex_unlock(&ab3100->access_mutex);
  89. return err;
  90. }
  91. static int set_register_interruptible(struct device *dev,
  92. u8 bank, u8 reg, u8 value)
  93. {
  94. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  95. return ab3100_set_register_interruptible(ab3100, reg, value);
  96. }
  97. /*
  98. * The test registers exist at an I2C bus address up one
  99. * from the ordinary base. They are not supposed to be used
  100. * in production code, but sometimes you have to do that
  101. * anyway. It's currently only used from this file so declare
  102. * it static and do not export.
  103. */
  104. static int ab3100_set_test_register_interruptible(struct ab3100 *ab3100,
  105. u8 reg, u8 regval)
  106. {
  107. u8 regandval[2] = {reg, regval};
  108. int err;
  109. err = mutex_lock_interruptible(&ab3100->access_mutex);
  110. if (err)
  111. return err;
  112. err = i2c_master_send(ab3100->testreg_client, regandval, 2);
  113. if (err < 0) {
  114. dev_err(ab3100->dev,
  115. "write error (write test register): %d\n",
  116. err);
  117. } else if (err != 2) {
  118. dev_err(ab3100->dev,
  119. "write error (write test register) "
  120. "%d bytes transferred (expected 2)\n",
  121. err);
  122. err = -EIO;
  123. } else {
  124. /* All is well */
  125. err = 0;
  126. }
  127. mutex_unlock(&ab3100->access_mutex);
  128. return err;
  129. }
  130. static int ab3100_get_register_interruptible(struct ab3100 *ab3100,
  131. u8 reg, u8 *regval)
  132. {
  133. int err;
  134. err = mutex_lock_interruptible(&ab3100->access_mutex);
  135. if (err)
  136. return err;
  137. /*
  138. * AB3100 require an I2C "stop" command between each message, else
  139. * it will not work. The only way of achieveing this with the
  140. * message transport layer is to send the read and write messages
  141. * separately.
  142. */
  143. err = i2c_master_send(ab3100->i2c_client, &reg, 1);
  144. if (err < 0) {
  145. dev_err(ab3100->dev,
  146. "write error (send register address): %d\n",
  147. err);
  148. goto get_reg_out_unlock;
  149. } else if (err != 1) {
  150. dev_err(ab3100->dev,
  151. "write error (send register address) "
  152. "%d bytes transferred (expected 1)\n",
  153. err);
  154. err = -EIO;
  155. goto get_reg_out_unlock;
  156. } else {
  157. /* All is well */
  158. err = 0;
  159. }
  160. err = i2c_master_recv(ab3100->i2c_client, regval, 1);
  161. if (err < 0) {
  162. dev_err(ab3100->dev,
  163. "write error (read register): %d\n",
  164. err);
  165. goto get_reg_out_unlock;
  166. } else if (err != 1) {
  167. dev_err(ab3100->dev,
  168. "write error (read register) "
  169. "%d bytes transferred (expected 1)\n",
  170. err);
  171. err = -EIO;
  172. goto get_reg_out_unlock;
  173. } else {
  174. /* All is well */
  175. err = 0;
  176. }
  177. get_reg_out_unlock:
  178. mutex_unlock(&ab3100->access_mutex);
  179. return err;
  180. }
  181. static int get_register_interruptible(struct device *dev, u8 bank, u8 reg,
  182. u8 *value)
  183. {
  184. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  185. return ab3100_get_register_interruptible(ab3100, reg, value);
  186. }
  187. static int ab3100_get_register_page_interruptible(struct ab3100 *ab3100,
  188. u8 first_reg, u8 *regvals, u8 numregs)
  189. {
  190. int err;
  191. if (ab3100->chip_id == 0xa0 ||
  192. ab3100->chip_id == 0xa1)
  193. /* These don't support paged reads */
  194. return -EIO;
  195. err = mutex_lock_interruptible(&ab3100->access_mutex);
  196. if (err)
  197. return err;
  198. /*
  199. * Paged read also require an I2C "stop" command.
  200. */
  201. err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
  202. if (err < 0) {
  203. dev_err(ab3100->dev,
  204. "write error (send first register address): %d\n",
  205. err);
  206. goto get_reg_page_out_unlock;
  207. } else if (err != 1) {
  208. dev_err(ab3100->dev,
  209. "write error (send first register address) "
  210. "%d bytes transferred (expected 1)\n",
  211. err);
  212. err = -EIO;
  213. goto get_reg_page_out_unlock;
  214. }
  215. err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
  216. if (err < 0) {
  217. dev_err(ab3100->dev,
  218. "write error (read register page): %d\n",
  219. err);
  220. goto get_reg_page_out_unlock;
  221. } else if (err != numregs) {
  222. dev_err(ab3100->dev,
  223. "write error (read register page) "
  224. "%d bytes transferred (expected %d)\n",
  225. err, numregs);
  226. err = -EIO;
  227. goto get_reg_page_out_unlock;
  228. }
  229. /* All is well */
  230. err = 0;
  231. get_reg_page_out_unlock:
  232. mutex_unlock(&ab3100->access_mutex);
  233. return err;
  234. }
  235. static int get_register_page_interruptible(struct device *dev, u8 bank,
  236. u8 first_reg, u8 *regvals, u8 numregs)
  237. {
  238. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  239. return ab3100_get_register_page_interruptible(ab3100,
  240. first_reg, regvals, numregs);
  241. }
  242. static int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100,
  243. u8 reg, u8 andmask, u8 ormask)
  244. {
  245. u8 regandval[2] = {reg, 0};
  246. int err;
  247. err = mutex_lock_interruptible(&ab3100->access_mutex);
  248. if (err)
  249. return err;
  250. /* First read out the target register */
  251. err = i2c_master_send(ab3100->i2c_client, &reg, 1);
  252. if (err < 0) {
  253. dev_err(ab3100->dev,
  254. "write error (maskset send address): %d\n",
  255. err);
  256. goto get_maskset_unlock;
  257. } else if (err != 1) {
  258. dev_err(ab3100->dev,
  259. "write error (maskset send address) "
  260. "%d bytes transferred (expected 1)\n",
  261. err);
  262. err = -EIO;
  263. goto get_maskset_unlock;
  264. }
  265. err = i2c_master_recv(ab3100->i2c_client, &regandval[1], 1);
  266. if (err < 0) {
  267. dev_err(ab3100->dev,
  268. "write error (maskset read register): %d\n",
  269. err);
  270. goto get_maskset_unlock;
  271. } else if (err != 1) {
  272. dev_err(ab3100->dev,
  273. "write error (maskset read register) "
  274. "%d bytes transferred (expected 1)\n",
  275. err);
  276. err = -EIO;
  277. goto get_maskset_unlock;
  278. }
  279. /* Modify the register */
  280. regandval[1] &= andmask;
  281. regandval[1] |= ormask;
  282. /* Write the register */
  283. err = i2c_master_send(ab3100->i2c_client, regandval, 2);
  284. if (err < 0) {
  285. dev_err(ab3100->dev,
  286. "write error (write register): %d\n",
  287. err);
  288. goto get_maskset_unlock;
  289. } else if (err != 2) {
  290. dev_err(ab3100->dev,
  291. "write error (write register) "
  292. "%d bytes transferred (expected 2)\n",
  293. err);
  294. err = -EIO;
  295. goto get_maskset_unlock;
  296. }
  297. /* All is well */
  298. err = 0;
  299. get_maskset_unlock:
  300. mutex_unlock(&ab3100->access_mutex);
  301. return err;
  302. }
  303. static int mask_and_set_register_interruptible(struct device *dev, u8 bank,
  304. u8 reg, u8 bitmask, u8 bitvalues)
  305. {
  306. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  307. return ab3100_mask_and_set_register_interruptible(ab3100,
  308. reg, bitmask, (bitmask & bitvalues));
  309. }
  310. /*
  311. * Register a simple callback for handling any AB3100 events.
  312. */
  313. int ab3100_event_register(struct ab3100 *ab3100,
  314. struct notifier_block *nb)
  315. {
  316. return blocking_notifier_chain_register(&ab3100->event_subscribers,
  317. nb);
  318. }
  319. EXPORT_SYMBOL(ab3100_event_register);
  320. /*
  321. * Remove a previously registered callback.
  322. */
  323. int ab3100_event_unregister(struct ab3100 *ab3100,
  324. struct notifier_block *nb)
  325. {
  326. return blocking_notifier_chain_unregister(&ab3100->event_subscribers,
  327. nb);
  328. }
  329. EXPORT_SYMBOL(ab3100_event_unregister);
  330. static int ab3100_event_registers_startup_state_get(struct device *dev,
  331. u8 *event)
  332. {
  333. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  334. if (!ab3100->startup_events_read)
  335. return -EAGAIN; /* Try again later */
  336. memcpy(event, ab3100->startup_events, 3);
  337. return 0;
  338. }
  339. static struct abx500_ops ab3100_ops = {
  340. .get_chip_id = ab3100_get_chip_id,
  341. .set_register = set_register_interruptible,
  342. .get_register = get_register_interruptible,
  343. .get_register_page = get_register_page_interruptible,
  344. .set_register_page = NULL,
  345. .mask_and_set_register = mask_and_set_register_interruptible,
  346. .event_registers_startup_state_get =
  347. ab3100_event_registers_startup_state_get,
  348. .startup_irq_enabled = NULL,
  349. };
  350. /*
  351. * This is a threaded interrupt handler so we can make some
  352. * I2C calls etc.
  353. */
  354. static irqreturn_t ab3100_irq_handler(int irq, void *data)
  355. {
  356. struct ab3100 *ab3100 = data;
  357. u8 event_regs[3];
  358. u32 fatevent;
  359. int err;
  360. add_interrupt_randomness(irq);
  361. err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
  362. event_regs, 3);
  363. if (err)
  364. goto err_event;
  365. fatevent = (event_regs[0] << 16) |
  366. (event_regs[1] << 8) |
  367. event_regs[2];
  368. if (!ab3100->startup_events_read) {
  369. ab3100->startup_events[0] = event_regs[0];
  370. ab3100->startup_events[1] = event_regs[1];
  371. ab3100->startup_events[2] = event_regs[2];
  372. ab3100->startup_events_read = true;
  373. }
  374. /*
  375. * The notified parties will have to mask out the events
  376. * they're interested in and react to them. They will be
  377. * notified on all events, then they use the fatevent value
  378. * to determine if they're interested.
  379. */
  380. blocking_notifier_call_chain(&ab3100->event_subscribers,
  381. fatevent, NULL);
  382. dev_dbg(ab3100->dev,
  383. "IRQ Event: 0x%08x\n", fatevent);
  384. return IRQ_HANDLED;
  385. err_event:
  386. dev_dbg(ab3100->dev,
  387. "error reading event status\n");
  388. return IRQ_HANDLED;
  389. }
  390. #ifdef CONFIG_DEBUG_FS
  391. /*
  392. * Some debugfs entries only exposed if we're using debug
  393. */
  394. static int ab3100_registers_print(struct seq_file *s, void *p)
  395. {
  396. struct ab3100 *ab3100 = s->private;
  397. u8 value;
  398. u8 reg;
  399. seq_printf(s, "AB3100 registers:\n");
  400. for (reg = 0; reg < 0xff; reg++) {
  401. ab3100_get_register_interruptible(ab3100, reg, &value);
  402. seq_printf(s, "[0x%x]: 0x%x\n", reg, value);
  403. }
  404. return 0;
  405. }
  406. static int ab3100_registers_open(struct inode *inode, struct file *file)
  407. {
  408. return single_open(file, ab3100_registers_print, inode->i_private);
  409. }
  410. static const struct file_operations ab3100_registers_fops = {
  411. .open = ab3100_registers_open,
  412. .read = seq_read,
  413. .llseek = seq_lseek,
  414. .release = single_release,
  415. .owner = THIS_MODULE,
  416. };
  417. struct ab3100_get_set_reg_priv {
  418. struct ab3100 *ab3100;
  419. bool mode;
  420. };
  421. static int ab3100_get_set_reg_open_file(struct inode *inode, struct file *file)
  422. {
  423. file->private_data = inode->i_private;
  424. return 0;
  425. }
  426. static ssize_t ab3100_get_set_reg(struct file *file,
  427. const char __user *user_buf,
  428. size_t count, loff_t *ppos)
  429. {
  430. struct ab3100_get_set_reg_priv *priv = file->private_data;
  431. struct ab3100 *ab3100 = priv->ab3100;
  432. char buf[32];
  433. ssize_t buf_size;
  434. int regp;
  435. unsigned long user_reg;
  436. int err;
  437. int i = 0;
  438. /* Get userspace string and assure termination */
  439. buf_size = min(count, (sizeof(buf)-1));
  440. if (copy_from_user(buf, user_buf, buf_size))
  441. return -EFAULT;
  442. buf[buf_size] = 0;
  443. /*
  444. * The idea is here to parse a string which is either
  445. * "0xnn" for reading a register, or "0xaa 0xbb" for
  446. * writing 0xbb to the register 0xaa. First move past
  447. * whitespace and then begin to parse the register.
  448. */
  449. while ((i < buf_size) && (buf[i] == ' '))
  450. i++;
  451. regp = i;
  452. /*
  453. * Advance pointer to end of string then terminate
  454. * the register string. This is needed to satisfy
  455. * the strict_strtoul() function.
  456. */
  457. while ((i < buf_size) && (buf[i] != ' '))
  458. i++;
  459. buf[i] = '\0';
  460. err = strict_strtoul(&buf[regp], 16, &user_reg);
  461. if (err)
  462. return err;
  463. if (user_reg > 0xff)
  464. return -EINVAL;
  465. /* Either we read or we write a register here */
  466. if (!priv->mode) {
  467. /* Reading */
  468. u8 reg = (u8) user_reg;
  469. u8 regvalue;
  470. ab3100_get_register_interruptible(ab3100, reg, &regvalue);
  471. dev_info(ab3100->dev,
  472. "debug read AB3100 reg[0x%02x]: 0x%02x\n",
  473. reg, regvalue);
  474. } else {
  475. int valp;
  476. unsigned long user_value;
  477. u8 reg = (u8) user_reg;
  478. u8 value;
  479. u8 regvalue;
  480. /*
  481. * Writing, we need some value to write to
  482. * the register so keep parsing the string
  483. * from userspace.
  484. */
  485. i++;
  486. while ((i < buf_size) && (buf[i] == ' '))
  487. i++;
  488. valp = i;
  489. while ((i < buf_size) && (buf[i] != ' '))
  490. i++;
  491. buf[i] = '\0';
  492. err = strict_strtoul(&buf[valp], 16, &user_value);
  493. if (err)
  494. return err;
  495. if (user_reg > 0xff)
  496. return -EINVAL;
  497. value = (u8) user_value;
  498. ab3100_set_register_interruptible(ab3100, reg, value);
  499. ab3100_get_register_interruptible(ab3100, reg, &regvalue);
  500. dev_info(ab3100->dev,
  501. "debug write reg[0x%02x] with 0x%02x, "
  502. "after readback: 0x%02x\n",
  503. reg, value, regvalue);
  504. }
  505. return buf_size;
  506. }
  507. static const struct file_operations ab3100_get_set_reg_fops = {
  508. .open = ab3100_get_set_reg_open_file,
  509. .write = ab3100_get_set_reg,
  510. .llseek = noop_llseek,
  511. };
  512. static struct dentry *ab3100_dir;
  513. static struct dentry *ab3100_reg_file;
  514. static struct ab3100_get_set_reg_priv ab3100_get_priv;
  515. static struct dentry *ab3100_get_reg_file;
  516. static struct ab3100_get_set_reg_priv ab3100_set_priv;
  517. static struct dentry *ab3100_set_reg_file;
  518. static void ab3100_setup_debugfs(struct ab3100 *ab3100)
  519. {
  520. int err;
  521. ab3100_dir = debugfs_create_dir("ab3100", NULL);
  522. if (!ab3100_dir)
  523. goto exit_no_debugfs;
  524. ab3100_reg_file = debugfs_create_file("registers",
  525. S_IRUGO, ab3100_dir, ab3100,
  526. &ab3100_registers_fops);
  527. if (!ab3100_reg_file) {
  528. err = -ENOMEM;
  529. goto exit_destroy_dir;
  530. }
  531. ab3100_get_priv.ab3100 = ab3100;
  532. ab3100_get_priv.mode = false;
  533. ab3100_get_reg_file = debugfs_create_file("get_reg",
  534. S_IWUSR, ab3100_dir, &ab3100_get_priv,
  535. &ab3100_get_set_reg_fops);
  536. if (!ab3100_get_reg_file) {
  537. err = -ENOMEM;
  538. goto exit_destroy_reg;
  539. }
  540. ab3100_set_priv.ab3100 = ab3100;
  541. ab3100_set_priv.mode = true;
  542. ab3100_set_reg_file = debugfs_create_file("set_reg",
  543. S_IWUSR, ab3100_dir, &ab3100_set_priv,
  544. &ab3100_get_set_reg_fops);
  545. if (!ab3100_set_reg_file) {
  546. err = -ENOMEM;
  547. goto exit_destroy_get_reg;
  548. }
  549. return;
  550. exit_destroy_get_reg:
  551. debugfs_remove(ab3100_get_reg_file);
  552. exit_destroy_reg:
  553. debugfs_remove(ab3100_reg_file);
  554. exit_destroy_dir:
  555. debugfs_remove(ab3100_dir);
  556. exit_no_debugfs:
  557. return;
  558. }
  559. static inline void ab3100_remove_debugfs(void)
  560. {
  561. debugfs_remove(ab3100_set_reg_file);
  562. debugfs_remove(ab3100_get_reg_file);
  563. debugfs_remove(ab3100_reg_file);
  564. debugfs_remove(ab3100_dir);
  565. }
  566. #else
  567. static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
  568. {
  569. }
  570. static inline void ab3100_remove_debugfs(void)
  571. {
  572. }
  573. #endif
  574. /*
  575. * Basic set-up, datastructure creation/destruction and I2C interface.
  576. * This sets up a default config in the AB3100 chip so that it
  577. * will work as expected.
  578. */
  579. struct ab3100_init_setting {
  580. u8 abreg;
  581. u8 setting;
  582. };
  583. static const struct ab3100_init_setting __devinitconst
  584. ab3100_init_settings[] = {
  585. {
  586. .abreg = AB3100_MCA,
  587. .setting = 0x01
  588. }, {
  589. .abreg = AB3100_MCB,
  590. .setting = 0x30
  591. }, {
  592. .abreg = AB3100_IMRA1,
  593. .setting = 0x00
  594. }, {
  595. .abreg = AB3100_IMRA2,
  596. .setting = 0xFF
  597. }, {
  598. .abreg = AB3100_IMRA3,
  599. .setting = 0x01
  600. }, {
  601. .abreg = AB3100_IMRB1,
  602. .setting = 0xBF
  603. }, {
  604. .abreg = AB3100_IMRB2,
  605. .setting = 0xFF
  606. }, {
  607. .abreg = AB3100_IMRB3,
  608. .setting = 0xFF
  609. }, {
  610. .abreg = AB3100_SUP,
  611. .setting = 0x00
  612. }, {
  613. .abreg = AB3100_DIS,
  614. .setting = 0xF0
  615. }, {
  616. .abreg = AB3100_D0C,
  617. .setting = 0x00
  618. }, {
  619. .abreg = AB3100_D1C,
  620. .setting = 0x00
  621. }, {
  622. .abreg = AB3100_D2C,
  623. .setting = 0x00
  624. }, {
  625. .abreg = AB3100_D3C,
  626. .setting = 0x00
  627. },
  628. };
  629. static int __devinit ab3100_setup(struct ab3100 *ab3100)
  630. {
  631. int err = 0;
  632. int i;
  633. for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
  634. err = ab3100_set_register_interruptible(ab3100,
  635. ab3100_init_settings[i].abreg,
  636. ab3100_init_settings[i].setting);
  637. if (err)
  638. goto exit_no_setup;
  639. }
  640. /*
  641. * Special trick to make the AB3100 use the 32kHz clock (RTC)
  642. * bit 3 in test register 0x02 is a special, undocumented test
  643. * register bit that only exist in AB3100 P1E
  644. */
  645. if (ab3100->chip_id == 0xc4) {
  646. dev_warn(ab3100->dev,
  647. "AB3100 P1E variant detected, "
  648. "forcing chip to 32KHz\n");
  649. err = ab3100_set_test_register_interruptible(ab3100,
  650. 0x02, 0x08);
  651. }
  652. exit_no_setup:
  653. return err;
  654. }
  655. /* The subdevices of the AB3100 */
  656. static struct mfd_cell ab3100_devs[] = {
  657. {
  658. .name = "ab3100-dac",
  659. .id = -1,
  660. },
  661. {
  662. .name = "ab3100-leds",
  663. .id = -1,
  664. },
  665. {
  666. .name = "ab3100-power",
  667. .id = -1,
  668. },
  669. {
  670. .name = "ab3100-regulators",
  671. .id = -1,
  672. },
  673. {
  674. .name = "ab3100-sim",
  675. .id = -1,
  676. },
  677. {
  678. .name = "ab3100-uart",
  679. .id = -1,
  680. },
  681. {
  682. .name = "ab3100-rtc",
  683. .id = -1,
  684. },
  685. {
  686. .name = "ab3100-charger",
  687. .id = -1,
  688. },
  689. {
  690. .name = "ab3100-boost",
  691. .id = -1,
  692. },
  693. {
  694. .name = "ab3100-adc",
  695. .id = -1,
  696. },
  697. {
  698. .name = "ab3100-fuelgauge",
  699. .id = -1,
  700. },
  701. {
  702. .name = "ab3100-vibrator",
  703. .id = -1,
  704. },
  705. {
  706. .name = "ab3100-otp",
  707. .id = -1,
  708. },
  709. {
  710. .name = "ab3100-codec",
  711. .id = -1,
  712. },
  713. };
  714. struct ab_family_id {
  715. u8 id;
  716. char *name;
  717. };
  718. static const struct ab_family_id ids[] __devinitconst = {
  719. /* AB3100 */
  720. {
  721. .id = 0xc0,
  722. .name = "P1A"
  723. }, {
  724. .id = 0xc1,
  725. .name = "P1B"
  726. }, {
  727. .id = 0xc2,
  728. .name = "P1C"
  729. }, {
  730. .id = 0xc3,
  731. .name = "P1D"
  732. }, {
  733. .id = 0xc4,
  734. .name = "P1E"
  735. }, {
  736. .id = 0xc5,
  737. .name = "P1F/R1A"
  738. }, {
  739. .id = 0xc6,
  740. .name = "P1G/R1A"
  741. }, {
  742. .id = 0xc7,
  743. .name = "P2A/R2A"
  744. }, {
  745. .id = 0xc8,
  746. .name = "P2B/R2B"
  747. },
  748. /* AB3000 variants, not supported */
  749. {
  750. .id = 0xa0
  751. }, {
  752. .id = 0xa1
  753. }, {
  754. .id = 0xa2
  755. }, {
  756. .id = 0xa3
  757. }, {
  758. .id = 0xa4
  759. }, {
  760. .id = 0xa5
  761. }, {
  762. .id = 0xa6
  763. }, {
  764. .id = 0xa7
  765. },
  766. /* Terminator */
  767. {
  768. .id = 0x00,
  769. },
  770. };
  771. static int __devinit ab3100_probe(struct i2c_client *client,
  772. const struct i2c_device_id *id)
  773. {
  774. struct ab3100 *ab3100;
  775. struct ab3100_platform_data *ab3100_plf_data =
  776. client->dev.platform_data;
  777. int err;
  778. int i;
  779. ab3100 = kzalloc(sizeof(struct ab3100), GFP_KERNEL);
  780. if (!ab3100) {
  781. dev_err(&client->dev, "could not allocate AB3100 device\n");
  782. return -ENOMEM;
  783. }
  784. /* Initialize data structure */
  785. mutex_init(&ab3100->access_mutex);
  786. BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
  787. ab3100->i2c_client = client;
  788. ab3100->dev = &ab3100->i2c_client->dev;
  789. i2c_set_clientdata(client, ab3100);
  790. /* Read chip ID register */
  791. err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
  792. &ab3100->chip_id);
  793. if (err) {
  794. dev_err(&client->dev,
  795. "could not communicate with the AB3100 analog "
  796. "baseband chip\n");
  797. goto exit_no_detect;
  798. }
  799. for (i = 0; ids[i].id != 0x0; i++) {
  800. if (ids[i].id == ab3100->chip_id) {
  801. if (ids[i].name != NULL) {
  802. snprintf(&ab3100->chip_name[0],
  803. sizeof(ab3100->chip_name) - 1,
  804. "AB3100 %s",
  805. ids[i].name);
  806. break;
  807. } else {
  808. dev_err(&client->dev,
  809. "AB3000 is not supported\n");
  810. goto exit_no_detect;
  811. }
  812. }
  813. }
  814. if (ids[i].id == 0x0) {
  815. dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
  816. ab3100->chip_id);
  817. dev_err(&client->dev, "accepting it anyway. Please update "
  818. "the driver.\n");
  819. goto exit_no_detect;
  820. }
  821. dev_info(&client->dev, "Detected chip: %s\n",
  822. &ab3100->chip_name[0]);
  823. /* Attach a second dummy i2c_client to the test register address */
  824. ab3100->testreg_client = i2c_new_dummy(client->adapter,
  825. client->addr + 1);
  826. if (!ab3100->testreg_client) {
  827. err = -ENOMEM;
  828. goto exit_no_testreg_client;
  829. }
  830. err = ab3100_setup(ab3100);
  831. if (err)
  832. goto exit_no_setup;
  833. err = request_threaded_irq(client->irq, NULL, ab3100_irq_handler,
  834. IRQF_ONESHOT, "ab3100-core", ab3100);
  835. /* This real unpredictable IRQ is of course sampled for entropy */
  836. rand_initialize_irq(client->irq);
  837. if (err)
  838. goto exit_no_irq;
  839. err = abx500_register_ops(&client->dev, &ab3100_ops);
  840. if (err)
  841. goto exit_no_ops;
  842. /* Set up and register the platform devices. */
  843. for (i = 0; i < ARRAY_SIZE(ab3100_devs); i++) {
  844. ab3100_devs[i].platform_data = ab3100_plf_data;
  845. ab3100_devs[i].pdata_size = sizeof(struct ab3100_platform_data);
  846. }
  847. err = mfd_add_devices(&client->dev, 0, ab3100_devs,
  848. ARRAY_SIZE(ab3100_devs), NULL, 0);
  849. ab3100_setup_debugfs(ab3100);
  850. return 0;
  851. exit_no_ops:
  852. exit_no_irq:
  853. exit_no_setup:
  854. i2c_unregister_device(ab3100->testreg_client);
  855. exit_no_testreg_client:
  856. exit_no_detect:
  857. kfree(ab3100);
  858. return err;
  859. }
  860. static int __devexit ab3100_remove(struct i2c_client *client)
  861. {
  862. struct ab3100 *ab3100 = i2c_get_clientdata(client);
  863. /* Unregister subdevices */
  864. mfd_remove_devices(&client->dev);
  865. ab3100_remove_debugfs();
  866. i2c_unregister_device(ab3100->testreg_client);
  867. /*
  868. * At this point, all subscribers should have unregistered
  869. * their notifiers so deactivate IRQ
  870. */
  871. free_irq(client->irq, ab3100);
  872. kfree(ab3100);
  873. return 0;
  874. }
  875. static const struct i2c_device_id ab3100_id[] = {
  876. { "ab3100", 0 },
  877. { }
  878. };
  879. MODULE_DEVICE_TABLE(i2c, ab3100_id);
  880. static struct i2c_driver ab3100_driver = {
  881. .driver = {
  882. .name = "ab3100",
  883. .owner = THIS_MODULE,
  884. },
  885. .id_table = ab3100_id,
  886. .probe = ab3100_probe,
  887. .remove = __devexit_p(ab3100_remove),
  888. };
  889. static int __init ab3100_i2c_init(void)
  890. {
  891. return i2c_add_driver(&ab3100_driver);
  892. }
  893. static void __exit ab3100_i2c_exit(void)
  894. {
  895. i2c_del_driver(&ab3100_driver);
  896. }
  897. subsys_initcall(ab3100_i2c_init);
  898. module_exit(ab3100_i2c_exit);
  899. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  900. MODULE_DESCRIPTION("AB3100 core driver");
  901. MODULE_LICENSE("GPL");