ab3100-core.c 22 KB

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