1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/mfd/si476x-i2c.c -- Core device driver for si476x MFD
6 * Copyright (C) 2012 Innovative Converged Devices(ICD)
7 * Copyright (C) 2013 Andrey Smirnov
9 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
11 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/err.h>
21 #include <linux/mfd/si476x-core.h>
23 #define SI476X_MAX_IO_ERRORS 10
24 #define SI476X_DRIVER_RDS_FIFO_DEPTH 128
27 * si476x_core_config_pinmux() - pin function configuration function
29 * @core: Core device structure
31 * Configure the functions of the pins of the radio chip.
33 * The function returns zero in case of succes or negative error code
36 static int si476x_core_config_pinmux(struct si476x_core *core)
39 dev_dbg(&core->client->dev, "Configuring pinmux\n");
40 err = si476x_core_cmd_dig_audio_pin_cfg(core,
46 dev_err(&core->client->dev,
47 "Failed to configure digital audio pins(err = %d)\n",
52 err = si476x_core_cmd_zif_pin_cfg(core,
58 dev_err(&core->client->dev,
59 "Failed to configure ZIF pins(err = %d)\n",
64 err = si476x_core_cmd_ic_link_gpo_ctl_pin_cfg(core,
70 dev_err(&core->client->dev,
71 "Failed to configure IC-Link/GPO pins(err = %d)\n",
76 err = si476x_core_cmd_ana_audio_pin_cfg(core,
79 dev_err(&core->client->dev,
80 "Failed to configure analog audio pins(err = %d)\n",
85 err = si476x_core_cmd_intb_pin_cfg(core,
89 dev_err(&core->client->dev,
90 "Failed to configure interrupt pins(err = %d)\n",
98 static inline void si476x_core_schedule_polling_work(struct si476x_core *core)
100 schedule_delayed_work(&core->status_monitor,
101 usecs_to_jiffies(SI476X_STATUS_POLL_US));
105 * si476x_core_start() - early chip startup function
106 * @core: Core device structure
107 * @soft: When set, this flag forces "soft" startup, where "soft"
108 * power down is the one done by sending appropriate command instead
109 * of using reset pin of the tuner
111 * Perform required startup sequence to correctly power
112 * up the chip and perform initial configuration. It does the
113 * following sequence of actions:
114 * 1. Claims and enables the power supplies VD and VIO1 required
115 * for I2C interface of the chip operation.
116 * 2. Waits for 100us, pulls the reset line up, enables irq,
117 * waits for another 100us as it is specified by the
119 * 3. Sends 'POWER_UP' command to the device with all provided
120 * information about power-up parameters.
121 * 4. Configures, pin multiplexor, disables digital audio and
122 * configures interrupt sources.
124 * The function returns zero in case of succes or negative error code
127 int si476x_core_start(struct si476x_core *core, bool soft)
129 struct i2c_client *client = core->client;
133 if (gpio_is_valid(core->gpio_reset))
134 gpio_set_value_cansleep(core->gpio_reset, 1);
137 enable_irq(client->irq);
142 atomic_set(&core->is_alive, 1);
143 si476x_core_schedule_polling_work(core);
147 enable_irq(client->irq);
149 atomic_set(&core->is_alive, 1);
150 si476x_core_schedule_polling_work(core);
154 err = si476x_core_cmd_power_up(core,
155 &core->power_up_parameters);
158 dev_err(&core->client->dev,
159 "Power up failure(err = %d)\n",
165 atomic_set(&core->is_alive, 1);
167 err = si476x_core_config_pinmux(core);
169 dev_err(&core->client->dev,
170 "Failed to configure pinmux(err = %d)\n",
176 err = regmap_write(core->regmap,
177 SI476X_PROP_INT_CTL_ENABLE,
182 dev_err(&core->client->dev,
183 "Failed to configure interrupt sources"
184 "(err = %d)\n", err);
193 atomic_set(&core->is_alive, 0);
196 disable_irq(client->irq);
198 cancel_delayed_work_sync(&core->status_monitor);
200 if (gpio_is_valid(core->gpio_reset))
201 gpio_set_value_cansleep(core->gpio_reset, 0);
205 EXPORT_SYMBOL_GPL(si476x_core_start);
208 * si476x_core_stop() - chip power-down function
209 * @core: Core device structure
210 * @soft: When set, function sends a POWER_DOWN command instead of
211 * bringing reset line low
213 * Power down the chip by performing following actions:
214 * 1. Disable IRQ or stop the polling worker
215 * 2. Send the POWER_DOWN command if the power down is soft or bring
216 * reset line low if not.
218 * The function returns zero in case of succes or negative error code
221 int si476x_core_stop(struct si476x_core *core, bool soft)
224 atomic_set(&core->is_alive, 0);
227 /* TODO: This probably shoud be a configurable option,
228 * so it is possible to have the chips keep their
229 * oscillators running
231 struct si476x_power_down_args args = {
234 err = si476x_core_cmd_power_down(core, &args);
237 /* We couldn't disable those before
238 * 'si476x_core_cmd_power_down' since we expect to get CTS
240 if (core->client->irq)
241 disable_irq(core->client->irq);
243 cancel_delayed_work_sync(&core->status_monitor);
246 if (gpio_is_valid(core->gpio_reset))
247 gpio_set_value_cansleep(core->gpio_reset, 0);
251 EXPORT_SYMBOL_GPL(si476x_core_stop);
254 * si476x_core_set_power_state() - set the level at which the power is
255 * supplied for the chip.
256 * @core: Core device structure
257 * @next_state: enum si476x_power_state describing power state to
260 * Switch on all the required power supplies
262 * This function returns 0 in case of suvccess and negative error code
265 int si476x_core_set_power_state(struct si476x_core *core,
266 enum si476x_power_state next_state)
269 It is not clear form the datasheet if it is possible to
270 work with device if not all power domains are operational.
271 So for now the power-up policy is "power-up all the things!"
275 if (core->power_state == SI476X_POWER_INCONSISTENT) {
276 dev_err(&core->client->dev,
277 "The device in inconsistent power state\n");
281 if (next_state != core->power_state) {
282 switch (next_state) {
283 case SI476X_POWER_UP_FULL:
284 err = regulator_bulk_enable(ARRAY_SIZE(core->supplies),
287 core->power_state = SI476X_POWER_INCONSISTENT;
291 * Startup timing diagram recommends to have a
292 * 100 us delay between enabling of the power
293 * supplies and turning the tuner on.
297 err = si476x_core_start(core, false);
299 goto disable_regulators;
301 core->power_state = next_state;
304 case SI476X_POWER_DOWN:
305 core->power_state = next_state;
306 err = si476x_core_stop(core, false);
308 core->power_state = SI476X_POWER_INCONSISTENT;
310 err = regulator_bulk_disable(ARRAY_SIZE(core->supplies),
313 core->power_state = SI476X_POWER_INCONSISTENT;
322 EXPORT_SYMBOL_GPL(si476x_core_set_power_state);
325 * si476x_core_report_drainer_stop() - mark the completion of the RDS
326 * buffer drain porcess by the worker.
328 * @core: Core device structure
330 static inline void si476x_core_report_drainer_stop(struct si476x_core *core)
332 mutex_lock(&core->rds_drainer_status_lock);
333 core->rds_drainer_is_working = false;
334 mutex_unlock(&core->rds_drainer_status_lock);
338 * si476x_core_start_rds_drainer_once() - start RDS drainer worker if
339 * ther is none working, do nothing otherwise
341 * @core: Datastructure corresponding to the chip.
343 static inline void si476x_core_start_rds_drainer_once(struct si476x_core *core)
345 mutex_lock(&core->rds_drainer_status_lock);
346 if (!core->rds_drainer_is_working) {
347 core->rds_drainer_is_working = true;
348 schedule_work(&core->rds_fifo_drainer);
350 mutex_unlock(&core->rds_drainer_status_lock);
353 * si476x_drain_rds_fifo() - RDS buffer drainer.
354 * @work: struct work_struct being ppassed to the function by the
357 * Drain the contents of the RDS FIFO of
359 static void si476x_core_drain_rds_fifo(struct work_struct *work)
363 struct si476x_core *core = container_of(work, struct si476x_core,
366 struct si476x_rds_status_report report;
368 si476x_core_lock(core);
369 err = si476x_core_cmd_fm_rds_status(core, true, false, false, &report);
371 int i = report.rdsfifoused;
372 dev_dbg(&core->client->dev,
373 "%d elements in RDS FIFO. Draining.\n", i);
375 err = si476x_core_cmd_fm_rds_status(core, false, false,
380 kfifo_in(&core->rds_fifo, report.rds,
382 dev_dbg(&core->client->dev, "RDS data:\n %*ph\n",
383 (int)sizeof(report.rds), report.rds);
385 dev_dbg(&core->client->dev, "Drrrrained!\n");
386 wake_up_interruptible(&core->rds_read_queue);
390 si476x_core_unlock(core);
391 si476x_core_report_drainer_stop(core);
395 * si476x_core_pronounce_dead()
397 * @core: Core device structure
399 * Mark the device as being dead and wake up all potentially waiting
400 * threads of execution.
403 static void si476x_core_pronounce_dead(struct si476x_core *core)
405 dev_info(&core->client->dev, "Core device is dead.\n");
407 atomic_set(&core->is_alive, 0);
409 /* Wake up al possible waiting processes */
410 wake_up_interruptible(&core->rds_read_queue);
412 atomic_set(&core->cts, 1);
413 wake_up(&core->command);
415 atomic_set(&core->stc, 1);
416 wake_up(&core->tuning);
420 * si476x_core_i2c_xfer()
422 * @core: Core device structure
423 * @type: Transfer type
424 * @buf: Transfer buffer for/with data
425 * @count: Transfer buffer size
427 * Perfrom and I2C transfer(either read or write) and keep a counter
428 * of I/O errors. If the error counter rises above the threshold
429 * pronounce device dead.
431 * The function returns zero on succes or negative error code on
434 int si476x_core_i2c_xfer(struct si476x_core *core,
435 enum si476x_i2c_type type,
436 char *buf, int count)
438 static int io_errors_count;
440 if (type == SI476X_I2C_SEND)
441 err = i2c_master_send(core->client, buf, count);
443 err = i2c_master_recv(core->client, buf, count);
446 if (io_errors_count++ > SI476X_MAX_IO_ERRORS)
447 si476x_core_pronounce_dead(core);
454 EXPORT_SYMBOL_GPL(si476x_core_i2c_xfer);
457 * si476x_get_status()
458 * @core: Core device structure
460 * Get the status byte of the core device by berforming one byte I2C
463 * The function returns a status value or a negative error code on
466 static int si476x_core_get_status(struct si476x_core *core)
469 int err = si476x_core_i2c_xfer(core, SI476X_I2C_RECV,
470 &response, sizeof(response));
472 return (err < 0) ? err : response;
476 * si476x_get_and_signal_status() - IRQ dispatcher
477 * @core: Core device structure
479 * Dispatch the arrived interrupt request based on the value of the
480 * status byte reported by the tuner.
483 static void si476x_core_get_and_signal_status(struct si476x_core *core)
485 int status = si476x_core_get_status(core);
487 dev_err(&core->client->dev, "Failed to get status\n");
491 if (status & SI476X_CTS) {
492 /* Unfortunately completions could not be used for
493 * signalling CTS since this flag cannot be cleared
494 * in status byte, and therefore once it becomes true
495 * multiple calls to 'complete' would cause the
496 * commands following the current one to be completed
497 * before they actually are */
498 dev_dbg(&core->client->dev, "[interrupt] CTSINT\n");
499 atomic_set(&core->cts, 1);
500 wake_up(&core->command);
503 if (status & SI476X_FM_RDS_INT) {
504 dev_dbg(&core->client->dev, "[interrupt] RDSINT\n");
505 si476x_core_start_rds_drainer_once(core);
508 if (status & SI476X_STC_INT) {
509 dev_dbg(&core->client->dev, "[interrupt] STCINT\n");
510 atomic_set(&core->stc, 1);
511 wake_up(&core->tuning);
515 static void si476x_core_poll_loop(struct work_struct *work)
517 struct si476x_core *core = SI476X_WORK_TO_CORE(work);
519 si476x_core_get_and_signal_status(core);
521 if (atomic_read(&core->is_alive))
522 si476x_core_schedule_polling_work(core);
525 static irqreturn_t si476x_core_interrupt(int irq, void *dev)
527 struct si476x_core *core = dev;
529 si476x_core_get_and_signal_status(core);
535 * si476x_firmware_version_to_revision()
536 * @core: Core device structure
537 * @major: Firmware major number
538 * @minor1: Firmware first minor number
539 * @minor2: Firmware second minor number
541 * Convert a chip's firmware version number into an offset that later
542 * will be used to as offset in "vtable" of tuner functions
544 * This function returns a positive offset in case of success and a -1
545 * in case of failure.
547 static int si476x_core_fwver_to_revision(struct si476x_core *core,
549 int minor1, int minor2)
552 case SI476X_FUNC_FM_RECEIVER:
555 return SI476X_REVISION_A10;
557 return SI476X_REVISION_A20;
559 return SI476X_REVISION_A30;
561 goto unknown_revision;
563 case SI476X_FUNC_AM_RECEIVER:
566 return SI476X_REVISION_A10;
568 return SI476X_REVISION_A20;
570 return SI476X_REVISION_A30;
572 goto unknown_revision;
574 case SI476X_FUNC_WB_RECEIVER:
577 return SI476X_REVISION_A10;
579 return SI476X_REVISION_A20;
581 return SI476X_REVISION_A30;
583 goto unknown_revision;
585 case SI476X_FUNC_BOOTLOADER:
586 default: /* FALLTHROUG */
592 dev_err(&core->client->dev,
593 "Unsupported version of the firmware: %d.%d.%d, "
594 "reverting to A10 compatible functions\n",
595 major, minor1, minor2);
597 return SI476X_REVISION_A10;
601 * si476x_get_revision_info()
602 * @core: Core device structure
604 * Get the firmware version number of the device. It is done in
605 * following three steps:
606 * 1. Power-up the device
607 * 2. Send the 'FUNC_INFO' command
608 * 3. Powering the device down.
610 * The function return zero on success and a negative error code on
613 static int si476x_core_get_revision_info(struct si476x_core *core)
616 struct si476x_func_info info;
618 si476x_core_lock(core);
619 rval = si476x_core_set_power_state(core, SI476X_POWER_UP_FULL);
623 rval = si476x_core_cmd_func_info(core, &info);
627 core->revision = si476x_core_fwver_to_revision(core, info.func,
629 info.firmware.minor[0],
630 info.firmware.minor[1]);
632 si476x_core_set_power_state(core, SI476X_POWER_DOWN);
634 si476x_core_unlock(core);
639 bool si476x_core_has_am(struct si476x_core *core)
641 return core->chip_id == SI476X_CHIP_SI4761 ||
642 core->chip_id == SI476X_CHIP_SI4764;
644 EXPORT_SYMBOL_GPL(si476x_core_has_am);
646 bool si476x_core_has_diversity(struct si476x_core *core)
648 return core->chip_id == SI476X_CHIP_SI4764;
650 EXPORT_SYMBOL_GPL(si476x_core_has_diversity);
652 bool si476x_core_is_a_secondary_tuner(struct si476x_core *core)
654 return si476x_core_has_diversity(core) &&
655 (core->diversity_mode == SI476X_PHDIV_SECONDARY_ANTENNA ||
656 core->diversity_mode == SI476X_PHDIV_SECONDARY_COMBINING);
658 EXPORT_SYMBOL_GPL(si476x_core_is_a_secondary_tuner);
660 bool si476x_core_is_a_primary_tuner(struct si476x_core *core)
662 return si476x_core_has_diversity(core) &&
663 (core->diversity_mode == SI476X_PHDIV_PRIMARY_ANTENNA ||
664 core->diversity_mode == SI476X_PHDIV_PRIMARY_COMBINING);
666 EXPORT_SYMBOL_GPL(si476x_core_is_a_primary_tuner);
668 bool si476x_core_is_in_am_receiver_mode(struct si476x_core *core)
670 return si476x_core_has_am(core) &&
671 (core->power_up_parameters.func == SI476X_FUNC_AM_RECEIVER);
673 EXPORT_SYMBOL_GPL(si476x_core_is_in_am_receiver_mode);
675 bool si476x_core_is_powered_up(struct si476x_core *core)
677 return core->power_state == SI476X_POWER_UP_FULL;
679 EXPORT_SYMBOL_GPL(si476x_core_is_powered_up);
681 static int si476x_core_probe(struct i2c_client *client,
682 const struct i2c_device_id *id)
685 struct si476x_core *core;
686 struct si476x_platform_data *pdata;
687 struct mfd_cell *cell;
690 core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
694 core->client = client;
696 core->regmap = devm_regmap_init_si476x(core);
697 if (IS_ERR(core->regmap)) {
698 rval = PTR_ERR(core->regmap);
699 dev_err(&client->dev,
700 "Failed to allocate register map: %d\n",
705 i2c_set_clientdata(client, core);
707 atomic_set(&core->is_alive, 0);
708 core->power_state = SI476X_POWER_DOWN;
710 pdata = dev_get_platdata(&client->dev);
712 memcpy(&core->power_up_parameters,
713 &pdata->power_up_parameters,
714 sizeof(core->power_up_parameters));
716 core->gpio_reset = -1;
717 if (gpio_is_valid(pdata->gpio_reset)) {
718 rval = gpio_request(pdata->gpio_reset, "si476x reset");
720 dev_err(&client->dev,
721 "Failed to request gpio: %d\n", rval);
724 core->gpio_reset = pdata->gpio_reset;
725 gpio_direction_output(core->gpio_reset, 0);
728 core->diversity_mode = pdata->diversity_mode;
729 memcpy(&core->pinmux, &pdata->pinmux,
730 sizeof(struct si476x_pinmux));
732 dev_err(&client->dev, "No platform data provided\n");
736 core->supplies[0].supply = "vd";
737 core->supplies[1].supply = "va";
738 core->supplies[2].supply = "vio1";
739 core->supplies[3].supply = "vio2";
741 rval = devm_regulator_bulk_get(&client->dev,
742 ARRAY_SIZE(core->supplies),
745 dev_err(&client->dev, "Failed to get all of the regulators\n");
749 mutex_init(&core->cmd_lock);
750 init_waitqueue_head(&core->command);
751 init_waitqueue_head(&core->tuning);
753 rval = kfifo_alloc(&core->rds_fifo,
754 SI476X_DRIVER_RDS_FIFO_DEPTH *
755 sizeof(struct v4l2_rds_data),
758 dev_err(&client->dev, "Could not allocate the FIFO\n");
761 mutex_init(&core->rds_drainer_status_lock);
762 init_waitqueue_head(&core->rds_read_queue);
763 INIT_WORK(&core->rds_fifo_drainer, si476x_core_drain_rds_fifo);
766 rval = devm_request_threaded_irq(&client->dev,
768 si476x_core_interrupt,
769 IRQF_TRIGGER_FALLING |
773 dev_err(&client->dev, "Could not request IRQ %d\n",
777 disable_irq(client->irq);
778 dev_dbg(&client->dev, "IRQ requested.\n");
780 core->rds_fifo_depth = 20;
782 INIT_DELAYED_WORK(&core->status_monitor,
783 si476x_core_poll_loop);
784 dev_info(&client->dev,
785 "No IRQ number specified, will use polling\n");
787 core->rds_fifo_depth = 5;
790 core->chip_id = id->driver_data;
792 rval = si476x_core_get_revision_info(core);
800 cell = &core->cells[SI476X_RADIO_CELL];
801 cell->name = "si476x-radio";
804 #ifdef CONFIG_SND_SOC_SI476X
805 if ((core->chip_id == SI476X_CHIP_SI4761 ||
806 core->chip_id == SI476X_CHIP_SI4764) &&
807 core->pinmux.dclk == SI476X_DCLK_DAUDIO &&
808 core->pinmux.dfs == SI476X_DFS_DAUDIO &&
809 core->pinmux.dout == SI476X_DOUT_I2S_OUTPUT &&
810 core->pinmux.xout == SI476X_XOUT_TRISTATE) {
811 cell = &core->cells[SI476X_CODEC_CELL];
812 cell->name = "si476x-codec";
816 rval = mfd_add_devices(&client->dev,
817 (client->adapter->nr << 8) + client->addr,
818 core->cells, cell_num,
824 kfifo_free(&core->rds_fifo);
827 if (gpio_is_valid(core->gpio_reset))
828 gpio_free(core->gpio_reset);
833 static int si476x_core_remove(struct i2c_client *client)
835 struct si476x_core *core = i2c_get_clientdata(client);
837 si476x_core_pronounce_dead(core);
838 mfd_remove_devices(&client->dev);
841 disable_irq(client->irq);
843 cancel_delayed_work_sync(&core->status_monitor);
845 kfifo_free(&core->rds_fifo);
847 if (gpio_is_valid(core->gpio_reset))
848 gpio_free(core->gpio_reset);
854 static const struct i2c_device_id si476x_id[] = {
855 { "si4761", SI476X_CHIP_SI4761 },
856 { "si4764", SI476X_CHIP_SI4764 },
857 { "si4768", SI476X_CHIP_SI4768 },
860 MODULE_DEVICE_TABLE(i2c, si476x_id);
862 static struct i2c_driver si476x_core_driver = {
864 .name = "si476x-core",
866 .probe = si476x_core_probe,
867 .remove = si476x_core_remove,
868 .id_table = si476x_id,
870 module_i2c_driver(si476x_core_driver);
873 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
874 MODULE_DESCRIPTION("Si4761/64/68 AM/FM MFD core device driver");
875 MODULE_LICENSE("GPL");