ab3100-core.c 23 KB

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