si476x-i2c.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/mfd/si476x-i2c.c -- Core device driver for si476x MFD
  4. * device
  5. *
  6. * Copyright (C) 2012 Innovative Converged Devices(ICD)
  7. * Copyright (C) 2013 Andrey Smirnov
  8. *
  9. * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/i2c.h>
  18. #include <linux/err.h>
  19. #include <linux/mfd/si476x-core.h>
  20. #define SI476X_MAX_IO_ERRORS 10
  21. #define SI476X_DRIVER_RDS_FIFO_DEPTH 128
  22. /**
  23. * si476x_core_config_pinmux() - pin function configuration function
  24. *
  25. * @core: Core device structure
  26. *
  27. * Configure the functions of the pins of the radio chip.
  28. *
  29. * The function returns zero in case of succes or negative error code
  30. * otherwise.
  31. */
  32. static int si476x_core_config_pinmux(struct si476x_core *core)
  33. {
  34. int err;
  35. dev_dbg(&core->client->dev, "Configuring pinmux\n");
  36. err = si476x_core_cmd_dig_audio_pin_cfg(core,
  37. core->pinmux.dclk,
  38. core->pinmux.dfs,
  39. core->pinmux.dout,
  40. core->pinmux.xout);
  41. if (err < 0) {
  42. dev_err(&core->client->dev,
  43. "Failed to configure digital audio pins(err = %d)\n",
  44. err);
  45. return err;
  46. }
  47. err = si476x_core_cmd_zif_pin_cfg(core,
  48. core->pinmux.iqclk,
  49. core->pinmux.iqfs,
  50. core->pinmux.iout,
  51. core->pinmux.qout);
  52. if (err < 0) {
  53. dev_err(&core->client->dev,
  54. "Failed to configure ZIF pins(err = %d)\n",
  55. err);
  56. return err;
  57. }
  58. err = si476x_core_cmd_ic_link_gpo_ctl_pin_cfg(core,
  59. core->pinmux.icin,
  60. core->pinmux.icip,
  61. core->pinmux.icon,
  62. core->pinmux.icop);
  63. if (err < 0) {
  64. dev_err(&core->client->dev,
  65. "Failed to configure IC-Link/GPO pins(err = %d)\n",
  66. err);
  67. return err;
  68. }
  69. err = si476x_core_cmd_ana_audio_pin_cfg(core,
  70. core->pinmux.lrout);
  71. if (err < 0) {
  72. dev_err(&core->client->dev,
  73. "Failed to configure analog audio pins(err = %d)\n",
  74. err);
  75. return err;
  76. }
  77. err = si476x_core_cmd_intb_pin_cfg(core,
  78. core->pinmux.intb,
  79. core->pinmux.a1);
  80. if (err < 0) {
  81. dev_err(&core->client->dev,
  82. "Failed to configure interrupt pins(err = %d)\n",
  83. err);
  84. return err;
  85. }
  86. return 0;
  87. }
  88. static inline void si476x_core_schedule_polling_work(struct si476x_core *core)
  89. {
  90. schedule_delayed_work(&core->status_monitor,
  91. usecs_to_jiffies(SI476X_STATUS_POLL_US));
  92. }
  93. /**
  94. * si476x_core_start() - early chip startup function
  95. * @core: Core device structure
  96. * @soft: When set, this flag forces "soft" startup, where "soft"
  97. * power down is the one done by sending appropriate command instead
  98. * of using reset pin of the tuner
  99. *
  100. * Perform required startup sequence to correctly power
  101. * up the chip and perform initial configuration. It does the
  102. * following sequence of actions:
  103. * 1. Claims and enables the power supplies VD and VIO1 required
  104. * for I2C interface of the chip operation.
  105. * 2. Waits for 100us, pulls the reset line up, enables irq,
  106. * waits for another 100us as it is specified by the
  107. * datasheet.
  108. * 3. Sends 'POWER_UP' command to the device with all provided
  109. * information about power-up parameters.
  110. * 4. Configures, pin multiplexor, disables digital audio and
  111. * configures interrupt sources.
  112. *
  113. * The function returns zero in case of succes or negative error code
  114. * otherwise.
  115. */
  116. int si476x_core_start(struct si476x_core *core, bool soft)
  117. {
  118. struct i2c_client *client = core->client;
  119. int err;
  120. if (!soft) {
  121. if (gpio_is_valid(core->gpio_reset))
  122. gpio_set_value_cansleep(core->gpio_reset, 1);
  123. if (client->irq)
  124. enable_irq(client->irq);
  125. udelay(100);
  126. if (!client->irq) {
  127. atomic_set(&core->is_alive, 1);
  128. si476x_core_schedule_polling_work(core);
  129. }
  130. } else {
  131. if (client->irq)
  132. enable_irq(client->irq);
  133. else {
  134. atomic_set(&core->is_alive, 1);
  135. si476x_core_schedule_polling_work(core);
  136. }
  137. }
  138. err = si476x_core_cmd_power_up(core,
  139. &core->power_up_parameters);
  140. if (err < 0) {
  141. dev_err(&core->client->dev,
  142. "Power up failure(err = %d)\n",
  143. err);
  144. goto disable_irq;
  145. }
  146. if (client->irq)
  147. atomic_set(&core->is_alive, 1);
  148. err = si476x_core_config_pinmux(core);
  149. if (err < 0) {
  150. dev_err(&core->client->dev,
  151. "Failed to configure pinmux(err = %d)\n",
  152. err);
  153. goto disable_irq;
  154. }
  155. if (client->irq) {
  156. err = regmap_write(core->regmap,
  157. SI476X_PROP_INT_CTL_ENABLE,
  158. SI476X_RDSIEN |
  159. SI476X_STCIEN |
  160. SI476X_CTSIEN);
  161. if (err < 0) {
  162. dev_err(&core->client->dev,
  163. "Failed to configure interrupt sources"
  164. "(err = %d)\n", err);
  165. goto disable_irq;
  166. }
  167. }
  168. return 0;
  169. disable_irq:
  170. if (err == -ENODEV)
  171. atomic_set(&core->is_alive, 0);
  172. if (client->irq)
  173. disable_irq(client->irq);
  174. else
  175. cancel_delayed_work_sync(&core->status_monitor);
  176. if (gpio_is_valid(core->gpio_reset))
  177. gpio_set_value_cansleep(core->gpio_reset, 0);
  178. return err;
  179. }
  180. EXPORT_SYMBOL_GPL(si476x_core_start);
  181. /**
  182. * si476x_core_stop() - chip power-down function
  183. * @core: Core device structure
  184. * @soft: When set, function sends a POWER_DOWN command instead of
  185. * bringing reset line low
  186. *
  187. * Power down the chip by performing following actions:
  188. * 1. Disable IRQ or stop the polling worker
  189. * 2. Send the POWER_DOWN command if the power down is soft or bring
  190. * reset line low if not.
  191. *
  192. * The function returns zero in case of succes or negative error code
  193. * otherwise.
  194. */
  195. int si476x_core_stop(struct si476x_core *core, bool soft)
  196. {
  197. int err = 0;
  198. atomic_set(&core->is_alive, 0);
  199. if (soft) {
  200. /* TODO: This probably shoud be a configurable option,
  201. * so it is possible to have the chips keep their
  202. * oscillators running
  203. */
  204. struct si476x_power_down_args args = {
  205. .xosc = false,
  206. };
  207. err = si476x_core_cmd_power_down(core, &args);
  208. }
  209. /* We couldn't disable those before
  210. * 'si476x_core_cmd_power_down' since we expect to get CTS
  211. * interrupt */
  212. if (core->client->irq)
  213. disable_irq(core->client->irq);
  214. else
  215. cancel_delayed_work_sync(&core->status_monitor);
  216. if (!soft) {
  217. if (gpio_is_valid(core->gpio_reset))
  218. gpio_set_value_cansleep(core->gpio_reset, 0);
  219. }
  220. return err;
  221. }
  222. EXPORT_SYMBOL_GPL(si476x_core_stop);
  223. /**
  224. * si476x_core_set_power_state() - set the level at which the power is
  225. * supplied for the chip.
  226. * @core: Core device structure
  227. * @next_state: enum si476x_power_state describing power state to
  228. * switch to.
  229. *
  230. * Switch on all the required power supplies
  231. *
  232. * This function returns 0 in case of suvccess and negative error code
  233. * otherwise.
  234. */
  235. int si476x_core_set_power_state(struct si476x_core *core,
  236. enum si476x_power_state next_state)
  237. {
  238. /*
  239. It is not clear form the datasheet if it is possible to
  240. work with device if not all power domains are operational.
  241. So for now the power-up policy is "power-up all the things!"
  242. */
  243. int err = 0;
  244. if (core->power_state == SI476X_POWER_INCONSISTENT) {
  245. dev_err(&core->client->dev,
  246. "The device in inconsistent power state\n");
  247. return -EINVAL;
  248. }
  249. if (next_state != core->power_state) {
  250. switch (next_state) {
  251. case SI476X_POWER_UP_FULL:
  252. err = regulator_bulk_enable(ARRAY_SIZE(core->supplies),
  253. core->supplies);
  254. if (err < 0) {
  255. core->power_state = SI476X_POWER_INCONSISTENT;
  256. break;
  257. }
  258. /*
  259. * Startup timing diagram recommends to have a
  260. * 100 us delay between enabling of the power
  261. * supplies and turning the tuner on.
  262. */
  263. udelay(100);
  264. err = si476x_core_start(core, false);
  265. if (err < 0)
  266. goto disable_regulators;
  267. core->power_state = next_state;
  268. break;
  269. case SI476X_POWER_DOWN:
  270. core->power_state = next_state;
  271. err = si476x_core_stop(core, false);
  272. if (err < 0)
  273. core->power_state = SI476X_POWER_INCONSISTENT;
  274. disable_regulators:
  275. err = regulator_bulk_disable(ARRAY_SIZE(core->supplies),
  276. core->supplies);
  277. if (err < 0)
  278. core->power_state = SI476X_POWER_INCONSISTENT;
  279. break;
  280. default:
  281. BUG();
  282. }
  283. }
  284. return err;
  285. }
  286. EXPORT_SYMBOL_GPL(si476x_core_set_power_state);
  287. /**
  288. * si476x_core_report_drainer_stop() - mark the completion of the RDS
  289. * buffer drain porcess by the worker.
  290. *
  291. * @core: Core device structure
  292. */
  293. static inline void si476x_core_report_drainer_stop(struct si476x_core *core)
  294. {
  295. mutex_lock(&core->rds_drainer_status_lock);
  296. core->rds_drainer_is_working = false;
  297. mutex_unlock(&core->rds_drainer_status_lock);
  298. }
  299. /**
  300. * si476x_core_start_rds_drainer_once() - start RDS drainer worker if
  301. * ther is none working, do nothing otherwise
  302. *
  303. * @core: Datastructure corresponding to the chip.
  304. */
  305. static inline void si476x_core_start_rds_drainer_once(struct si476x_core *core)
  306. {
  307. mutex_lock(&core->rds_drainer_status_lock);
  308. if (!core->rds_drainer_is_working) {
  309. core->rds_drainer_is_working = true;
  310. schedule_work(&core->rds_fifo_drainer);
  311. }
  312. mutex_unlock(&core->rds_drainer_status_lock);
  313. }
  314. /**
  315. * si476x_drain_rds_fifo() - RDS buffer drainer.
  316. * @work: struct work_struct being ppassed to the function by the
  317. * kernel.
  318. *
  319. * Drain the contents of the RDS FIFO of
  320. */
  321. static void si476x_core_drain_rds_fifo(struct work_struct *work)
  322. {
  323. int err;
  324. struct si476x_core *core = container_of(work, struct si476x_core,
  325. rds_fifo_drainer);
  326. struct si476x_rds_status_report report;
  327. si476x_core_lock(core);
  328. err = si476x_core_cmd_fm_rds_status(core, true, false, false, &report);
  329. if (!err) {
  330. int i = report.rdsfifoused;
  331. dev_dbg(&core->client->dev,
  332. "%d elements in RDS FIFO. Draining.\n", i);
  333. for (; i > 0; --i) {
  334. err = si476x_core_cmd_fm_rds_status(core, false, false,
  335. (i == 1), &report);
  336. if (err < 0)
  337. goto unlock;
  338. kfifo_in(&core->rds_fifo, report.rds,
  339. sizeof(report.rds));
  340. dev_dbg(&core->client->dev, "RDS data:\n %*ph\n",
  341. (int)sizeof(report.rds), report.rds);
  342. }
  343. dev_dbg(&core->client->dev, "Drrrrained!\n");
  344. wake_up_interruptible(&core->rds_read_queue);
  345. }
  346. unlock:
  347. si476x_core_unlock(core);
  348. si476x_core_report_drainer_stop(core);
  349. }
  350. /**
  351. * si476x_core_pronounce_dead()
  352. *
  353. * @core: Core device structure
  354. *
  355. * Mark the device as being dead and wake up all potentially waiting
  356. * threads of execution.
  357. *
  358. */
  359. static void si476x_core_pronounce_dead(struct si476x_core *core)
  360. {
  361. dev_info(&core->client->dev, "Core device is dead.\n");
  362. atomic_set(&core->is_alive, 0);
  363. /* Wake up al possible waiting processes */
  364. wake_up_interruptible(&core->rds_read_queue);
  365. atomic_set(&core->cts, 1);
  366. wake_up(&core->command);
  367. atomic_set(&core->stc, 1);
  368. wake_up(&core->tuning);
  369. }
  370. /**
  371. * si476x_core_i2c_xfer()
  372. *
  373. * @core: Core device structure
  374. * @type: Transfer type
  375. * @buf: Transfer buffer for/with data
  376. * @count: Transfer buffer size
  377. *
  378. * Perfrom and I2C transfer(either read or write) and keep a counter
  379. * of I/O errors. If the error counter rises above the threshold
  380. * pronounce device dead.
  381. *
  382. * The function returns zero on succes or negative error code on
  383. * failure.
  384. */
  385. int si476x_core_i2c_xfer(struct si476x_core *core,
  386. enum si476x_i2c_type type,
  387. char *buf, int count)
  388. {
  389. static int io_errors_count;
  390. int err;
  391. if (type == SI476X_I2C_SEND)
  392. err = i2c_master_send(core->client, buf, count);
  393. else
  394. err = i2c_master_recv(core->client, buf, count);
  395. if (err < 0) {
  396. if (io_errors_count++ > SI476X_MAX_IO_ERRORS)
  397. si476x_core_pronounce_dead(core);
  398. } else {
  399. io_errors_count = 0;
  400. }
  401. return err;
  402. }
  403. EXPORT_SYMBOL_GPL(si476x_core_i2c_xfer);
  404. /**
  405. * si476x_get_status()
  406. * @core: Core device structure
  407. *
  408. * Get the status byte of the core device by berforming one byte I2C
  409. * read.
  410. *
  411. * The function returns a status value or a negative error code on
  412. * error.
  413. */
  414. static int si476x_core_get_status(struct si476x_core *core)
  415. {
  416. u8 response;
  417. int err = si476x_core_i2c_xfer(core, SI476X_I2C_RECV,
  418. &response, sizeof(response));
  419. return (err < 0) ? err : response;
  420. }
  421. /**
  422. * si476x_get_and_signal_status() - IRQ dispatcher
  423. * @core: Core device structure
  424. *
  425. * Dispatch the arrived interrupt request based on the value of the
  426. * status byte reported by the tuner.
  427. *
  428. */
  429. static void si476x_core_get_and_signal_status(struct si476x_core *core)
  430. {
  431. int status = si476x_core_get_status(core);
  432. if (status < 0) {
  433. dev_err(&core->client->dev, "Failed to get status\n");
  434. return;
  435. }
  436. if (status & SI476X_CTS) {
  437. /* Unfortunately completions could not be used for
  438. * signalling CTS since this flag cannot be cleared
  439. * in status byte, and therefore once it becomes true
  440. * multiple calls to 'complete' would cause the
  441. * commands following the current one to be completed
  442. * before they actually are */
  443. dev_dbg(&core->client->dev, "[interrupt] CTSINT\n");
  444. atomic_set(&core->cts, 1);
  445. wake_up(&core->command);
  446. }
  447. if (status & SI476X_FM_RDS_INT) {
  448. dev_dbg(&core->client->dev, "[interrupt] RDSINT\n");
  449. si476x_core_start_rds_drainer_once(core);
  450. }
  451. if (status & SI476X_STC_INT) {
  452. dev_dbg(&core->client->dev, "[interrupt] STCINT\n");
  453. atomic_set(&core->stc, 1);
  454. wake_up(&core->tuning);
  455. }
  456. }
  457. static void si476x_core_poll_loop(struct work_struct *work)
  458. {
  459. struct si476x_core *core = SI476X_WORK_TO_CORE(work);
  460. si476x_core_get_and_signal_status(core);
  461. if (atomic_read(&core->is_alive))
  462. si476x_core_schedule_polling_work(core);
  463. }
  464. static irqreturn_t si476x_core_interrupt(int irq, void *dev)
  465. {
  466. struct si476x_core *core = dev;
  467. si476x_core_get_and_signal_status(core);
  468. return IRQ_HANDLED;
  469. }
  470. /**
  471. * si476x_firmware_version_to_revision()
  472. * @core: Core device structure
  473. * @func: Selects the boot function of the device:
  474. * *_BOOTLOADER - Boot loader
  475. * *_FM_RECEIVER - FM receiver
  476. * *_AM_RECEIVER - AM receiver
  477. * *_WB_RECEIVER - Weatherband receiver
  478. * @major: Firmware major number
  479. * @minor1: Firmware first minor number
  480. * @minor2: Firmware second minor number
  481. *
  482. * Convert a chip's firmware version number into an offset that later
  483. * will be used to as offset in "vtable" of tuner functions
  484. *
  485. * This function returns a positive offset in case of success and a -1
  486. * in case of failure.
  487. */
  488. static int si476x_core_fwver_to_revision(struct si476x_core *core,
  489. int func, int major,
  490. int minor1, int minor2)
  491. {
  492. switch (func) {
  493. case SI476X_FUNC_FM_RECEIVER:
  494. switch (major) {
  495. case 5:
  496. return SI476X_REVISION_A10;
  497. case 8:
  498. return SI476X_REVISION_A20;
  499. case 10:
  500. return SI476X_REVISION_A30;
  501. default:
  502. goto unknown_revision;
  503. }
  504. case SI476X_FUNC_AM_RECEIVER:
  505. switch (major) {
  506. case 5:
  507. return SI476X_REVISION_A10;
  508. case 7:
  509. return SI476X_REVISION_A20;
  510. case 9:
  511. return SI476X_REVISION_A30;
  512. default:
  513. goto unknown_revision;
  514. }
  515. case SI476X_FUNC_WB_RECEIVER:
  516. switch (major) {
  517. case 3:
  518. return SI476X_REVISION_A10;
  519. case 5:
  520. return SI476X_REVISION_A20;
  521. case 7:
  522. return SI476X_REVISION_A30;
  523. default:
  524. goto unknown_revision;
  525. }
  526. case SI476X_FUNC_BOOTLOADER:
  527. default: /* FALLTHROUGH */
  528. BUG();
  529. return -1;
  530. }
  531. unknown_revision:
  532. dev_err(&core->client->dev,
  533. "Unsupported version of the firmware: %d.%d.%d, "
  534. "reverting to A10 compatible functions\n",
  535. major, minor1, minor2);
  536. return SI476X_REVISION_A10;
  537. }
  538. /**
  539. * si476x_get_revision_info()
  540. * @core: Core device structure
  541. *
  542. * Get the firmware version number of the device. It is done in
  543. * following three steps:
  544. * 1. Power-up the device
  545. * 2. Send the 'FUNC_INFO' command
  546. * 3. Powering the device down.
  547. *
  548. * The function return zero on success and a negative error code on
  549. * failure.
  550. */
  551. static int si476x_core_get_revision_info(struct si476x_core *core)
  552. {
  553. int rval;
  554. struct si476x_func_info info;
  555. si476x_core_lock(core);
  556. rval = si476x_core_set_power_state(core, SI476X_POWER_UP_FULL);
  557. if (rval < 0)
  558. goto exit;
  559. rval = si476x_core_cmd_func_info(core, &info);
  560. if (rval < 0)
  561. goto power_down;
  562. core->revision = si476x_core_fwver_to_revision(core, info.func,
  563. info.firmware.major,
  564. info.firmware.minor[0],
  565. info.firmware.minor[1]);
  566. power_down:
  567. si476x_core_set_power_state(core, SI476X_POWER_DOWN);
  568. exit:
  569. si476x_core_unlock(core);
  570. return rval;
  571. }
  572. bool si476x_core_has_am(struct si476x_core *core)
  573. {
  574. return core->chip_id == SI476X_CHIP_SI4761 ||
  575. core->chip_id == SI476X_CHIP_SI4764;
  576. }
  577. EXPORT_SYMBOL_GPL(si476x_core_has_am);
  578. bool si476x_core_has_diversity(struct si476x_core *core)
  579. {
  580. return core->chip_id == SI476X_CHIP_SI4764;
  581. }
  582. EXPORT_SYMBOL_GPL(si476x_core_has_diversity);
  583. bool si476x_core_is_a_secondary_tuner(struct si476x_core *core)
  584. {
  585. return si476x_core_has_diversity(core) &&
  586. (core->diversity_mode == SI476X_PHDIV_SECONDARY_ANTENNA ||
  587. core->diversity_mode == SI476X_PHDIV_SECONDARY_COMBINING);
  588. }
  589. EXPORT_SYMBOL_GPL(si476x_core_is_a_secondary_tuner);
  590. bool si476x_core_is_a_primary_tuner(struct si476x_core *core)
  591. {
  592. return si476x_core_has_diversity(core) &&
  593. (core->diversity_mode == SI476X_PHDIV_PRIMARY_ANTENNA ||
  594. core->diversity_mode == SI476X_PHDIV_PRIMARY_COMBINING);
  595. }
  596. EXPORT_SYMBOL_GPL(si476x_core_is_a_primary_tuner);
  597. bool si476x_core_is_in_am_receiver_mode(struct si476x_core *core)
  598. {
  599. return si476x_core_has_am(core) &&
  600. (core->power_up_parameters.func == SI476X_FUNC_AM_RECEIVER);
  601. }
  602. EXPORT_SYMBOL_GPL(si476x_core_is_in_am_receiver_mode);
  603. bool si476x_core_is_powered_up(struct si476x_core *core)
  604. {
  605. return core->power_state == SI476X_POWER_UP_FULL;
  606. }
  607. EXPORT_SYMBOL_GPL(si476x_core_is_powered_up);
  608. static int si476x_core_probe(struct i2c_client *client,
  609. const struct i2c_device_id *id)
  610. {
  611. int rval;
  612. struct si476x_core *core;
  613. struct si476x_platform_data *pdata;
  614. struct mfd_cell *cell;
  615. int cell_num;
  616. core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
  617. if (!core)
  618. return -ENOMEM;
  619. core->client = client;
  620. core->regmap = devm_regmap_init_si476x(core);
  621. if (IS_ERR(core->regmap)) {
  622. rval = PTR_ERR(core->regmap);
  623. dev_err(&client->dev,
  624. "Failed to allocate register map: %d\n",
  625. rval);
  626. return rval;
  627. }
  628. i2c_set_clientdata(client, core);
  629. atomic_set(&core->is_alive, 0);
  630. core->power_state = SI476X_POWER_DOWN;
  631. pdata = dev_get_platdata(&client->dev);
  632. if (pdata) {
  633. memcpy(&core->power_up_parameters,
  634. &pdata->power_up_parameters,
  635. sizeof(core->power_up_parameters));
  636. core->gpio_reset = -1;
  637. if (gpio_is_valid(pdata->gpio_reset)) {
  638. rval = gpio_request(pdata->gpio_reset, "si476x reset");
  639. if (rval) {
  640. dev_err(&client->dev,
  641. "Failed to request gpio: %d\n", rval);
  642. return rval;
  643. }
  644. core->gpio_reset = pdata->gpio_reset;
  645. gpio_direction_output(core->gpio_reset, 0);
  646. }
  647. core->diversity_mode = pdata->diversity_mode;
  648. memcpy(&core->pinmux, &pdata->pinmux,
  649. sizeof(struct si476x_pinmux));
  650. } else {
  651. dev_err(&client->dev, "No platform data provided\n");
  652. return -EINVAL;
  653. }
  654. core->supplies[0].supply = "vd";
  655. core->supplies[1].supply = "va";
  656. core->supplies[2].supply = "vio1";
  657. core->supplies[3].supply = "vio2";
  658. rval = devm_regulator_bulk_get(&client->dev,
  659. ARRAY_SIZE(core->supplies),
  660. core->supplies);
  661. if (rval) {
  662. dev_err(&client->dev, "Failed to get all of the regulators\n");
  663. goto free_gpio;
  664. }
  665. mutex_init(&core->cmd_lock);
  666. init_waitqueue_head(&core->command);
  667. init_waitqueue_head(&core->tuning);
  668. rval = kfifo_alloc(&core->rds_fifo,
  669. SI476X_DRIVER_RDS_FIFO_DEPTH *
  670. sizeof(struct v4l2_rds_data),
  671. GFP_KERNEL);
  672. if (rval) {
  673. dev_err(&client->dev, "Could not allocate the FIFO\n");
  674. goto free_gpio;
  675. }
  676. mutex_init(&core->rds_drainer_status_lock);
  677. init_waitqueue_head(&core->rds_read_queue);
  678. INIT_WORK(&core->rds_fifo_drainer, si476x_core_drain_rds_fifo);
  679. if (client->irq) {
  680. rval = devm_request_threaded_irq(&client->dev,
  681. client->irq, NULL,
  682. si476x_core_interrupt,
  683. IRQF_TRIGGER_FALLING |
  684. IRQF_ONESHOT,
  685. client->name, core);
  686. if (rval < 0) {
  687. dev_err(&client->dev, "Could not request IRQ %d\n",
  688. client->irq);
  689. goto free_kfifo;
  690. }
  691. disable_irq(client->irq);
  692. dev_dbg(&client->dev, "IRQ requested.\n");
  693. core->rds_fifo_depth = 20;
  694. } else {
  695. INIT_DELAYED_WORK(&core->status_monitor,
  696. si476x_core_poll_loop);
  697. dev_info(&client->dev,
  698. "No IRQ number specified, will use polling\n");
  699. core->rds_fifo_depth = 5;
  700. }
  701. core->chip_id = id->driver_data;
  702. rval = si476x_core_get_revision_info(core);
  703. if (rval < 0) {
  704. rval = -ENODEV;
  705. goto free_kfifo;
  706. }
  707. cell_num = 0;
  708. cell = &core->cells[SI476X_RADIO_CELL];
  709. cell->name = "si476x-radio";
  710. cell_num++;
  711. #ifdef CONFIG_SND_SOC_SI476X
  712. if ((core->chip_id == SI476X_CHIP_SI4761 ||
  713. core->chip_id == SI476X_CHIP_SI4764) &&
  714. core->pinmux.dclk == SI476X_DCLK_DAUDIO &&
  715. core->pinmux.dfs == SI476X_DFS_DAUDIO &&
  716. core->pinmux.dout == SI476X_DOUT_I2S_OUTPUT &&
  717. core->pinmux.xout == SI476X_XOUT_TRISTATE) {
  718. cell = &core->cells[SI476X_CODEC_CELL];
  719. cell->name = "si476x-codec";
  720. cell_num++;
  721. }
  722. #endif
  723. rval = mfd_add_devices(&client->dev,
  724. (client->adapter->nr << 8) + client->addr,
  725. core->cells, cell_num,
  726. NULL, 0, NULL);
  727. if (!rval)
  728. return 0;
  729. free_kfifo:
  730. kfifo_free(&core->rds_fifo);
  731. free_gpio:
  732. if (gpio_is_valid(core->gpio_reset))
  733. gpio_free(core->gpio_reset);
  734. return rval;
  735. }
  736. static int si476x_core_remove(struct i2c_client *client)
  737. {
  738. struct si476x_core *core = i2c_get_clientdata(client);
  739. si476x_core_pronounce_dead(core);
  740. mfd_remove_devices(&client->dev);
  741. if (client->irq)
  742. disable_irq(client->irq);
  743. else
  744. cancel_delayed_work_sync(&core->status_monitor);
  745. kfifo_free(&core->rds_fifo);
  746. if (gpio_is_valid(core->gpio_reset))
  747. gpio_free(core->gpio_reset);
  748. return 0;
  749. }
  750. static const struct i2c_device_id si476x_id[] = {
  751. { "si4761", SI476X_CHIP_SI4761 },
  752. { "si4764", SI476X_CHIP_SI4764 },
  753. { "si4768", SI476X_CHIP_SI4768 },
  754. { },
  755. };
  756. MODULE_DEVICE_TABLE(i2c, si476x_id);
  757. static struct i2c_driver si476x_core_driver = {
  758. .driver = {
  759. .name = "si476x-core",
  760. },
  761. .probe = si476x_core_probe,
  762. .remove = si476x_core_remove,
  763. .id_table = si476x_id,
  764. };
  765. module_i2c_driver(si476x_core_driver);
  766. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  767. MODULE_DESCRIPTION("Si4761/64/68 AM/FM MFD core device driver");
  768. MODULE_LICENSE("GPL");