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