ab3100-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
  361. event_regs, 3);
  362. if (err)
  363. goto err_event;
  364. fatevent = (event_regs[0] << 16) |
  365. (event_regs[1] << 8) |
  366. event_regs[2];
  367. if (!ab3100->startup_events_read) {
  368. ab3100->startup_events[0] = event_regs[0];
  369. ab3100->startup_events[1] = event_regs[1];
  370. ab3100->startup_events[2] = event_regs[2];
  371. ab3100->startup_events_read = true;
  372. }
  373. /*
  374. * The notified parties will have to mask out the events
  375. * they're interested in and react to them. They will be
  376. * notified on all events, then they use the fatevent value
  377. * to determine if they're interested.
  378. */
  379. blocking_notifier_call_chain(&ab3100->event_subscribers,
  380. fatevent, NULL);
  381. dev_dbg(ab3100->dev,
  382. "IRQ Event: 0x%08x\n", fatevent);
  383. return IRQ_HANDLED;
  384. err_event:
  385. dev_dbg(ab3100->dev,
  386. "error reading event status\n");
  387. return IRQ_HANDLED;
  388. }
  389. #ifdef CONFIG_DEBUG_FS
  390. /*
  391. * Some debugfs entries only exposed if we're using debug
  392. */
  393. static int ab3100_registers_print(struct seq_file *s, void *p)
  394. {
  395. struct ab3100 *ab3100 = s->private;
  396. u8 value;
  397. u8 reg;
  398. seq_printf(s, "AB3100 registers:\n");
  399. for (reg = 0; reg < 0xff; reg++) {
  400. ab3100_get_register_interruptible(ab3100, reg, &value);
  401. seq_printf(s, "[0x%x]: 0x%x\n", reg, value);
  402. }
  403. return 0;
  404. }
  405. static int ab3100_registers_open(struct inode *inode, struct file *file)
  406. {
  407. return single_open(file, ab3100_registers_print, inode->i_private);
  408. }
  409. static const struct file_operations ab3100_registers_fops = {
  410. .open = ab3100_registers_open,
  411. .read = seq_read,
  412. .llseek = seq_lseek,
  413. .release = single_release,
  414. .owner = THIS_MODULE,
  415. };
  416. struct ab3100_get_set_reg_priv {
  417. struct ab3100 *ab3100;
  418. bool mode;
  419. };
  420. static ssize_t ab3100_get_set_reg(struct file *file,
  421. const char __user *user_buf,
  422. size_t count, loff_t *ppos)
  423. {
  424. struct ab3100_get_set_reg_priv *priv = file->private_data;
  425. struct ab3100 *ab3100 = priv->ab3100;
  426. char buf[32];
  427. ssize_t buf_size;
  428. int regp;
  429. unsigned long user_reg;
  430. int err;
  431. int i = 0;
  432. /* Get userspace string and assure termination */
  433. buf_size = min(count, (sizeof(buf)-1));
  434. if (copy_from_user(buf, user_buf, buf_size))
  435. return -EFAULT;
  436. buf[buf_size] = 0;
  437. /*
  438. * The idea is here to parse a string which is either
  439. * "0xnn" for reading a register, or "0xaa 0xbb" for
  440. * writing 0xbb to the register 0xaa. First move past
  441. * whitespace and then begin to parse the register.
  442. */
  443. while ((i < buf_size) && (buf[i] == ' '))
  444. i++;
  445. regp = i;
  446. /*
  447. * Advance pointer to end of string then terminate
  448. * the register string. This is needed to satisfy
  449. * the strict_strtoul() function.
  450. */
  451. while ((i < buf_size) && (buf[i] != ' '))
  452. i++;
  453. buf[i] = '\0';
  454. err = strict_strtoul(&buf[regp], 16, &user_reg);
  455. if (err)
  456. return err;
  457. if (user_reg > 0xff)
  458. return -EINVAL;
  459. /* Either we read or we write a register here */
  460. if (!priv->mode) {
  461. /* Reading */
  462. u8 reg = (u8) user_reg;
  463. u8 regvalue;
  464. ab3100_get_register_interruptible(ab3100, reg, &regvalue);
  465. dev_info(ab3100->dev,
  466. "debug read AB3100 reg[0x%02x]: 0x%02x\n",
  467. reg, regvalue);
  468. } else {
  469. int valp;
  470. unsigned long user_value;
  471. u8 reg = (u8) user_reg;
  472. u8 value;
  473. u8 regvalue;
  474. /*
  475. * Writing, we need some value to write to
  476. * the register so keep parsing the string
  477. * from userspace.
  478. */
  479. i++;
  480. while ((i < buf_size) && (buf[i] == ' '))
  481. i++;
  482. valp = i;
  483. while ((i < buf_size) && (buf[i] != ' '))
  484. i++;
  485. buf[i] = '\0';
  486. err = strict_strtoul(&buf[valp], 16, &user_value);
  487. if (err)
  488. return err;
  489. if (user_reg > 0xff)
  490. return -EINVAL;
  491. value = (u8) user_value;
  492. ab3100_set_register_interruptible(ab3100, reg, value);
  493. ab3100_get_register_interruptible(ab3100, reg, &regvalue);
  494. dev_info(ab3100->dev,
  495. "debug write reg[0x%02x] with 0x%02x, "
  496. "after readback: 0x%02x\n",
  497. reg, value, regvalue);
  498. }
  499. return buf_size;
  500. }
  501. static const struct file_operations ab3100_get_set_reg_fops = {
  502. .open = simple_open,
  503. .write = ab3100_get_set_reg,
  504. .llseek = noop_llseek,
  505. };
  506. static struct dentry *ab3100_dir;
  507. static struct dentry *ab3100_reg_file;
  508. static struct ab3100_get_set_reg_priv ab3100_get_priv;
  509. static struct dentry *ab3100_get_reg_file;
  510. static struct ab3100_get_set_reg_priv ab3100_set_priv;
  511. static struct dentry *ab3100_set_reg_file;
  512. static void ab3100_setup_debugfs(struct ab3100 *ab3100)
  513. {
  514. int err;
  515. ab3100_dir = debugfs_create_dir("ab3100", NULL);
  516. if (!ab3100_dir)
  517. goto exit_no_debugfs;
  518. ab3100_reg_file = debugfs_create_file("registers",
  519. S_IRUGO, ab3100_dir, ab3100,
  520. &ab3100_registers_fops);
  521. if (!ab3100_reg_file) {
  522. err = -ENOMEM;
  523. goto exit_destroy_dir;
  524. }
  525. ab3100_get_priv.ab3100 = ab3100;
  526. ab3100_get_priv.mode = false;
  527. ab3100_get_reg_file = debugfs_create_file("get_reg",
  528. S_IWUSR, ab3100_dir, &ab3100_get_priv,
  529. &ab3100_get_set_reg_fops);
  530. if (!ab3100_get_reg_file) {
  531. err = -ENOMEM;
  532. goto exit_destroy_reg;
  533. }
  534. ab3100_set_priv.ab3100 = ab3100;
  535. ab3100_set_priv.mode = true;
  536. ab3100_set_reg_file = debugfs_create_file("set_reg",
  537. S_IWUSR, ab3100_dir, &ab3100_set_priv,
  538. &ab3100_get_set_reg_fops);
  539. if (!ab3100_set_reg_file) {
  540. err = -ENOMEM;
  541. goto exit_destroy_get_reg;
  542. }
  543. return;
  544. exit_destroy_get_reg:
  545. debugfs_remove(ab3100_get_reg_file);
  546. exit_destroy_reg:
  547. debugfs_remove(ab3100_reg_file);
  548. exit_destroy_dir:
  549. debugfs_remove(ab3100_dir);
  550. exit_no_debugfs:
  551. return;
  552. }
  553. static inline void ab3100_remove_debugfs(void)
  554. {
  555. debugfs_remove(ab3100_set_reg_file);
  556. debugfs_remove(ab3100_get_reg_file);
  557. debugfs_remove(ab3100_reg_file);
  558. debugfs_remove(ab3100_dir);
  559. }
  560. #else
  561. static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
  562. {
  563. }
  564. static inline void ab3100_remove_debugfs(void)
  565. {
  566. }
  567. #endif
  568. /*
  569. * Basic set-up, datastructure creation/destruction and I2C interface.
  570. * This sets up a default config in the AB3100 chip so that it
  571. * will work as expected.
  572. */
  573. struct ab3100_init_setting {
  574. u8 abreg;
  575. u8 setting;
  576. };
  577. static const struct ab3100_init_setting __devinitconst
  578. ab3100_init_settings[] = {
  579. {
  580. .abreg = AB3100_MCA,
  581. .setting = 0x01
  582. }, {
  583. .abreg = AB3100_MCB,
  584. .setting = 0x30
  585. }, {
  586. .abreg = AB3100_IMRA1,
  587. .setting = 0x00
  588. }, {
  589. .abreg = AB3100_IMRA2,
  590. .setting = 0xFF
  591. }, {
  592. .abreg = AB3100_IMRA3,
  593. .setting = 0x01
  594. }, {
  595. .abreg = AB3100_IMRB1,
  596. .setting = 0xBF
  597. }, {
  598. .abreg = AB3100_IMRB2,
  599. .setting = 0xFF
  600. }, {
  601. .abreg = AB3100_IMRB3,
  602. .setting = 0xFF
  603. }, {
  604. .abreg = AB3100_SUP,
  605. .setting = 0x00
  606. }, {
  607. .abreg = AB3100_DIS,
  608. .setting = 0xF0
  609. }, {
  610. .abreg = AB3100_D0C,
  611. .setting = 0x00
  612. }, {
  613. .abreg = AB3100_D1C,
  614. .setting = 0x00
  615. }, {
  616. .abreg = AB3100_D2C,
  617. .setting = 0x00
  618. }, {
  619. .abreg = AB3100_D3C,
  620. .setting = 0x00
  621. },
  622. };
  623. static int __devinit ab3100_setup(struct ab3100 *ab3100)
  624. {
  625. int err = 0;
  626. int i;
  627. for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
  628. err = ab3100_set_register_interruptible(ab3100,
  629. ab3100_init_settings[i].abreg,
  630. ab3100_init_settings[i].setting);
  631. if (err)
  632. goto exit_no_setup;
  633. }
  634. /*
  635. * Special trick to make the AB3100 use the 32kHz clock (RTC)
  636. * bit 3 in test register 0x02 is a special, undocumented test
  637. * register bit that only exist in AB3100 P1E
  638. */
  639. if (ab3100->chip_id == 0xc4) {
  640. dev_warn(ab3100->dev,
  641. "AB3100 P1E variant detected, "
  642. "forcing chip to 32KHz\n");
  643. err = ab3100_set_test_register_interruptible(ab3100,
  644. 0x02, 0x08);
  645. }
  646. exit_no_setup:
  647. return err;
  648. }
  649. /* The subdevices of the AB3100 */
  650. static struct mfd_cell ab3100_devs[] = {
  651. {
  652. .name = "ab3100-dac",
  653. .id = -1,
  654. },
  655. {
  656. .name = "ab3100-leds",
  657. .id = -1,
  658. },
  659. {
  660. .name = "ab3100-power",
  661. .id = -1,
  662. },
  663. {
  664. .name = "ab3100-regulators",
  665. .id = -1,
  666. },
  667. {
  668. .name = "ab3100-sim",
  669. .id = -1,
  670. },
  671. {
  672. .name = "ab3100-uart",
  673. .id = -1,
  674. },
  675. {
  676. .name = "ab3100-rtc",
  677. .id = -1,
  678. },
  679. {
  680. .name = "ab3100-charger",
  681. .id = -1,
  682. },
  683. {
  684. .name = "ab3100-boost",
  685. .id = -1,
  686. },
  687. {
  688. .name = "ab3100-adc",
  689. .id = -1,
  690. },
  691. {
  692. .name = "ab3100-fuelgauge",
  693. .id = -1,
  694. },
  695. {
  696. .name = "ab3100-vibrator",
  697. .id = -1,
  698. },
  699. {
  700. .name = "ab3100-otp",
  701. .id = -1,
  702. },
  703. {
  704. .name = "ab3100-codec",
  705. .id = -1,
  706. },
  707. };
  708. struct ab_family_id {
  709. u8 id;
  710. char *name;
  711. };
  712. static const struct ab_family_id ids[] __devinitconst = {
  713. /* AB3100 */
  714. {
  715. .id = 0xc0,
  716. .name = "P1A"
  717. }, {
  718. .id = 0xc1,
  719. .name = "P1B"
  720. }, {
  721. .id = 0xc2,
  722. .name = "P1C"
  723. }, {
  724. .id = 0xc3,
  725. .name = "P1D"
  726. }, {
  727. .id = 0xc4,
  728. .name = "P1E"
  729. }, {
  730. .id = 0xc5,
  731. .name = "P1F/R1A"
  732. }, {
  733. .id = 0xc6,
  734. .name = "P1G/R1A"
  735. }, {
  736. .id = 0xc7,
  737. .name = "P2A/R2A"
  738. }, {
  739. .id = 0xc8,
  740. .name = "P2B/R2B"
  741. },
  742. /* AB3000 variants, not supported */
  743. {
  744. .id = 0xa0
  745. }, {
  746. .id = 0xa1
  747. }, {
  748. .id = 0xa2
  749. }, {
  750. .id = 0xa3
  751. }, {
  752. .id = 0xa4
  753. }, {
  754. .id = 0xa5
  755. }, {
  756. .id = 0xa6
  757. }, {
  758. .id = 0xa7
  759. },
  760. /* Terminator */
  761. {
  762. .id = 0x00,
  763. },
  764. };
  765. static int __devinit ab3100_probe(struct i2c_client *client,
  766. const struct i2c_device_id *id)
  767. {
  768. struct ab3100 *ab3100;
  769. struct ab3100_platform_data *ab3100_plf_data =
  770. client->dev.platform_data;
  771. int err;
  772. int i;
  773. ab3100 = kzalloc(sizeof(struct ab3100), GFP_KERNEL);
  774. if (!ab3100) {
  775. dev_err(&client->dev, "could not allocate AB3100 device\n");
  776. return -ENOMEM;
  777. }
  778. /* Initialize data structure */
  779. mutex_init(&ab3100->access_mutex);
  780. BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
  781. ab3100->i2c_client = client;
  782. ab3100->dev = &ab3100->i2c_client->dev;
  783. i2c_set_clientdata(client, ab3100);
  784. /* Read chip ID register */
  785. err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
  786. &ab3100->chip_id);
  787. if (err) {
  788. dev_err(&client->dev,
  789. "could not communicate with the AB3100 analog "
  790. "baseband chip\n");
  791. goto exit_no_detect;
  792. }
  793. for (i = 0; ids[i].id != 0x0; i++) {
  794. if (ids[i].id == ab3100->chip_id) {
  795. if (ids[i].name != NULL) {
  796. snprintf(&ab3100->chip_name[0],
  797. sizeof(ab3100->chip_name) - 1,
  798. "AB3100 %s",
  799. ids[i].name);
  800. break;
  801. } else {
  802. dev_err(&client->dev,
  803. "AB3000 is not supported\n");
  804. goto exit_no_detect;
  805. }
  806. }
  807. }
  808. if (ids[i].id == 0x0) {
  809. dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
  810. ab3100->chip_id);
  811. dev_err(&client->dev, "accepting it anyway. Please update "
  812. "the driver.\n");
  813. goto exit_no_detect;
  814. }
  815. dev_info(&client->dev, "Detected chip: %s\n",
  816. &ab3100->chip_name[0]);
  817. /* Attach a second dummy i2c_client to the test register address */
  818. ab3100->testreg_client = i2c_new_dummy(client->adapter,
  819. client->addr + 1);
  820. if (!ab3100->testreg_client) {
  821. err = -ENOMEM;
  822. goto exit_no_testreg_client;
  823. }
  824. err = ab3100_setup(ab3100);
  825. if (err)
  826. goto exit_no_setup;
  827. err = request_threaded_irq(client->irq, NULL, ab3100_irq_handler,
  828. IRQF_ONESHOT, "ab3100-core", ab3100);
  829. if (err)
  830. goto exit_no_irq;
  831. err = abx500_register_ops(&client->dev, &ab3100_ops);
  832. if (err)
  833. goto exit_no_ops;
  834. /* Set up and register the platform devices. */
  835. for (i = 0; i < ARRAY_SIZE(ab3100_devs); i++) {
  836. ab3100_devs[i].platform_data = ab3100_plf_data;
  837. ab3100_devs[i].pdata_size = sizeof(struct ab3100_platform_data);
  838. }
  839. err = mfd_add_devices(&client->dev, 0, ab3100_devs,
  840. ARRAY_SIZE(ab3100_devs), NULL, 0);
  841. ab3100_setup_debugfs(ab3100);
  842. return 0;
  843. exit_no_ops:
  844. exit_no_irq:
  845. exit_no_setup:
  846. i2c_unregister_device(ab3100->testreg_client);
  847. exit_no_testreg_client:
  848. exit_no_detect:
  849. kfree(ab3100);
  850. return err;
  851. }
  852. static int __devexit ab3100_remove(struct i2c_client *client)
  853. {
  854. struct ab3100 *ab3100 = i2c_get_clientdata(client);
  855. /* Unregister subdevices */
  856. mfd_remove_devices(&client->dev);
  857. ab3100_remove_debugfs();
  858. i2c_unregister_device(ab3100->testreg_client);
  859. /*
  860. * At this point, all subscribers should have unregistered
  861. * their notifiers so deactivate IRQ
  862. */
  863. free_irq(client->irq, ab3100);
  864. kfree(ab3100);
  865. return 0;
  866. }
  867. static const struct i2c_device_id ab3100_id[] = {
  868. { "ab3100", 0 },
  869. { }
  870. };
  871. MODULE_DEVICE_TABLE(i2c, ab3100_id);
  872. static struct i2c_driver ab3100_driver = {
  873. .driver = {
  874. .name = "ab3100",
  875. .owner = THIS_MODULE,
  876. },
  877. .id_table = ab3100_id,
  878. .probe = ab3100_probe,
  879. .remove = __devexit_p(ab3100_remove),
  880. };
  881. static int __init ab3100_i2c_init(void)
  882. {
  883. return i2c_add_driver(&ab3100_driver);
  884. }
  885. static void __exit ab3100_i2c_exit(void)
  886. {
  887. i2c_del_driver(&ab3100_driver);
  888. }
  889. subsys_initcall(ab3100_i2c_init);
  890. module_exit(ab3100_i2c_exit);
  891. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  892. MODULE_DESCRIPTION("AB3100 core driver");
  893. MODULE_LICENSE("GPL");