This source file includes following definitions.
- si476x_core_config_pinmux
- si476x_core_schedule_polling_work
- si476x_core_start
- si476x_core_stop
- si476x_core_set_power_state
- si476x_core_report_drainer_stop
- si476x_core_start_rds_drainer_once
- si476x_core_drain_rds_fifo
- si476x_core_pronounce_dead
- si476x_core_i2c_xfer
- si476x_core_get_status
- si476x_core_get_and_signal_status
- si476x_core_poll_loop
- si476x_core_interrupt
- si476x_core_fwver_to_revision
- si476x_core_get_revision_info
- si476x_core_has_am
- si476x_core_has_diversity
- si476x_core_is_a_secondary_tuner
- si476x_core_is_a_primary_tuner
- si476x_core_is_in_am_receiver_mode
- si476x_core_is_powered_up
- si476x_core_probe
- si476x_core_remove
1
2
3
4
5
6
7
8
9
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.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
28
29
30
31
32
33
34
35
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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 if (gpio_is_valid(core->gpio_reset))
134 gpio_set_value_cansleep(core->gpio_reset, 1);
135
136 if (client->irq)
137 enable_irq(client->irq);
138
139 udelay(100);
140
141 if (!client->irq) {
142 atomic_set(&core->is_alive, 1);
143 si476x_core_schedule_polling_work(core);
144 }
145 } else {
146 if (client->irq)
147 enable_irq(client->irq);
148 else {
149 atomic_set(&core->is_alive, 1);
150 si476x_core_schedule_polling_work(core);
151 }
152 }
153
154 err = si476x_core_cmd_power_up(core,
155 &core->power_up_parameters);
156
157 if (err < 0) {
158 dev_err(&core->client->dev,
159 "Power up failure(err = %d)\n",
160 err);
161 goto disable_irq;
162 }
163
164 if (client->irq)
165 atomic_set(&core->is_alive, 1);
166
167 err = si476x_core_config_pinmux(core);
168 if (err < 0) {
169 dev_err(&core->client->dev,
170 "Failed to configure pinmux(err = %d)\n",
171 err);
172 goto disable_irq;
173 }
174
175 if (client->irq) {
176 err = regmap_write(core->regmap,
177 SI476X_PROP_INT_CTL_ENABLE,
178 SI476X_RDSIEN |
179 SI476X_STCIEN |
180 SI476X_CTSIEN);
181 if (err < 0) {
182 dev_err(&core->client->dev,
183 "Failed to configure interrupt sources"
184 "(err = %d)\n", err);
185 goto disable_irq;
186 }
187 }
188
189 return 0;
190
191 disable_irq:
192 if (err == -ENODEV)
193 atomic_set(&core->is_alive, 0);
194
195 if (client->irq)
196 disable_irq(client->irq);
197 else
198 cancel_delayed_work_sync(&core->status_monitor);
199
200 if (gpio_is_valid(core->gpio_reset))
201 gpio_set_value_cansleep(core->gpio_reset, 0);
202
203 return err;
204 }
205 EXPORT_SYMBOL_GPL(si476x_core_start);
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 int si476x_core_stop(struct si476x_core *core, bool soft)
222 {
223 int err = 0;
224 atomic_set(&core->is_alive, 0);
225
226 if (soft) {
227
228
229
230
231 struct si476x_power_down_args args = {
232 .xosc = false,
233 };
234 err = si476x_core_cmd_power_down(core, &args);
235 }
236
237
238
239
240 if (core->client->irq)
241 disable_irq(core->client->irq);
242 else
243 cancel_delayed_work_sync(&core->status_monitor);
244
245 if (!soft) {
246 if (gpio_is_valid(core->gpio_reset))
247 gpio_set_value_cansleep(core->gpio_reset, 0);
248 }
249 return err;
250 }
251 EXPORT_SYMBOL_GPL(si476x_core_stop);
252
253
254
255
256
257
258
259
260
261
262
263
264
265 int si476x_core_set_power_state(struct si476x_core *core,
266 enum si476x_power_state next_state)
267 {
268
269
270
271
272
273 int err = 0;
274
275 if (core->power_state == SI476X_POWER_INCONSISTENT) {
276 dev_err(&core->client->dev,
277 "The device in inconsistent power state\n");
278 return -EINVAL;
279 }
280
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),
285 core->supplies);
286 if (err < 0) {
287 core->power_state = SI476X_POWER_INCONSISTENT;
288 break;
289 }
290
291
292
293
294
295 udelay(100);
296
297 err = si476x_core_start(core, false);
298 if (err < 0)
299 goto disable_regulators;
300
301 core->power_state = next_state;
302 break;
303
304 case SI476X_POWER_DOWN:
305 core->power_state = next_state;
306 err = si476x_core_stop(core, false);
307 if (err < 0)
308 core->power_state = SI476X_POWER_INCONSISTENT;
309 disable_regulators:
310 err = regulator_bulk_disable(ARRAY_SIZE(core->supplies),
311 core->supplies);
312 if (err < 0)
313 core->power_state = SI476X_POWER_INCONSISTENT;
314 break;
315 default:
316 BUG();
317 }
318 }
319
320 return err;
321 }
322 EXPORT_SYMBOL_GPL(si476x_core_set_power_state);
323
324
325
326
327
328
329
330 static inline void si476x_core_report_drainer_stop(struct si476x_core *core)
331 {
332 mutex_lock(&core->rds_drainer_status_lock);
333 core->rds_drainer_is_working = false;
334 mutex_unlock(&core->rds_drainer_status_lock);
335 }
336
337
338
339
340
341
342
343 static inline void si476x_core_start_rds_drainer_once(struct si476x_core *core)
344 {
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);
349 }
350 mutex_unlock(&core->rds_drainer_status_lock);
351 }
352
353
354
355
356
357
358
359 static void si476x_core_drain_rds_fifo(struct work_struct *work)
360 {
361 int err;
362
363 struct si476x_core *core = container_of(work, struct si476x_core,
364 rds_fifo_drainer);
365
366 struct si476x_rds_status_report report;
367
368 si476x_core_lock(core);
369 err = si476x_core_cmd_fm_rds_status(core, true, false, false, &report);
370 if (!err) {
371 int i = report.rdsfifoused;
372 dev_dbg(&core->client->dev,
373 "%d elements in RDS FIFO. Draining.\n", i);
374 for (; i > 0; --i) {
375 err = si476x_core_cmd_fm_rds_status(core, false, false,
376 (i == 1), &report);
377 if (err < 0)
378 goto unlock;
379
380 kfifo_in(&core->rds_fifo, report.rds,
381 sizeof(report.rds));
382 dev_dbg(&core->client->dev, "RDS data:\n %*ph\n",
383 (int)sizeof(report.rds), report.rds);
384 }
385 dev_dbg(&core->client->dev, "Drrrrained!\n");
386 wake_up_interruptible(&core->rds_read_queue);
387 }
388
389 unlock:
390 si476x_core_unlock(core);
391 si476x_core_report_drainer_stop(core);
392 }
393
394
395
396
397
398
399
400
401
402
403 static void si476x_core_pronounce_dead(struct si476x_core *core)
404 {
405 dev_info(&core->client->dev, "Core device is dead.\n");
406
407 atomic_set(&core->is_alive, 0);
408
409
410 wake_up_interruptible(&core->rds_read_queue);
411
412 atomic_set(&core->cts, 1);
413 wake_up(&core->command);
414
415 atomic_set(&core->stc, 1);
416 wake_up(&core->tuning);
417 }
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434 int si476x_core_i2c_xfer(struct si476x_core *core,
435 enum si476x_i2c_type type,
436 char *buf, int count)
437 {
438 static int io_errors_count;
439 int err;
440 if (type == SI476X_I2C_SEND)
441 err = i2c_master_send(core->client, buf, count);
442 else
443 err = i2c_master_recv(core->client, buf, count);
444
445 if (err < 0) {
446 if (io_errors_count++ > SI476X_MAX_IO_ERRORS)
447 si476x_core_pronounce_dead(core);
448 } else {
449 io_errors_count = 0;
450 }
451
452 return err;
453 }
454 EXPORT_SYMBOL_GPL(si476x_core_i2c_xfer);
455
456
457
458
459
460
461
462
463
464
465
466 static int si476x_core_get_status(struct si476x_core *core)
467 {
468 u8 response;
469 int err = si476x_core_i2c_xfer(core, SI476X_I2C_RECV,
470 &response, sizeof(response));
471
472 return (err < 0) ? err : response;
473 }
474
475
476
477
478
479
480
481
482
483 static void si476x_core_get_and_signal_status(struct si476x_core *core)
484 {
485 int status = si476x_core_get_status(core);
486 if (status < 0) {
487 dev_err(&core->client->dev, "Failed to get status\n");
488 return;
489 }
490
491 if (status & SI476X_CTS) {
492
493
494
495
496
497
498 dev_dbg(&core->client->dev, "[interrupt] CTSINT\n");
499 atomic_set(&core->cts, 1);
500 wake_up(&core->command);
501 }
502
503 if (status & SI476X_FM_RDS_INT) {
504 dev_dbg(&core->client->dev, "[interrupt] RDSINT\n");
505 si476x_core_start_rds_drainer_once(core);
506 }
507
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);
512 }
513 }
514
515 static void si476x_core_poll_loop(struct work_struct *work)
516 {
517 struct si476x_core *core = SI476X_WORK_TO_CORE(work);
518
519 si476x_core_get_and_signal_status(core);
520
521 if (atomic_read(&core->is_alive))
522 si476x_core_schedule_polling_work(core);
523 }
524
525 static irqreturn_t si476x_core_interrupt(int irq, void *dev)
526 {
527 struct si476x_core *core = dev;
528
529 si476x_core_get_and_signal_status(core);
530
531 return IRQ_HANDLED;
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547 static int si476x_core_fwver_to_revision(struct si476x_core *core,
548 int func, int major,
549 int minor1, int minor2)
550 {
551 switch (func) {
552 case SI476X_FUNC_FM_RECEIVER:
553 switch (major) {
554 case 5:
555 return SI476X_REVISION_A10;
556 case 8:
557 return SI476X_REVISION_A20;
558 case 10:
559 return SI476X_REVISION_A30;
560 default:
561 goto unknown_revision;
562 }
563 case SI476X_FUNC_AM_RECEIVER:
564 switch (major) {
565 case 5:
566 return SI476X_REVISION_A10;
567 case 7:
568 return SI476X_REVISION_A20;
569 case 9:
570 return SI476X_REVISION_A30;
571 default:
572 goto unknown_revision;
573 }
574 case SI476X_FUNC_WB_RECEIVER:
575 switch (major) {
576 case 3:
577 return SI476X_REVISION_A10;
578 case 5:
579 return SI476X_REVISION_A20;
580 case 7:
581 return SI476X_REVISION_A30;
582 default:
583 goto unknown_revision;
584 }
585 case SI476X_FUNC_BOOTLOADER:
586 default:
587 BUG();
588 return -1;
589 }
590
591 unknown_revision:
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);
596
597 return SI476X_REVISION_A10;
598 }
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613 static int si476x_core_get_revision_info(struct si476x_core *core)
614 {
615 int rval;
616 struct si476x_func_info info;
617
618 si476x_core_lock(core);
619 rval = si476x_core_set_power_state(core, SI476X_POWER_UP_FULL);
620 if (rval < 0)
621 goto exit;
622
623 rval = si476x_core_cmd_func_info(core, &info);
624 if (rval < 0)
625 goto power_down;
626
627 core->revision = si476x_core_fwver_to_revision(core, info.func,
628 info.firmware.major,
629 info.firmware.minor[0],
630 info.firmware.minor[1]);
631 power_down:
632 si476x_core_set_power_state(core, SI476X_POWER_DOWN);
633 exit:
634 si476x_core_unlock(core);
635
636 return rval;
637 }
638
639 bool si476x_core_has_am(struct si476x_core *core)
640 {
641 return core->chip_id == SI476X_CHIP_SI4761 ||
642 core->chip_id == SI476X_CHIP_SI4764;
643 }
644 EXPORT_SYMBOL_GPL(si476x_core_has_am);
645
646 bool si476x_core_has_diversity(struct si476x_core *core)
647 {
648 return core->chip_id == SI476X_CHIP_SI4764;
649 }
650 EXPORT_SYMBOL_GPL(si476x_core_has_diversity);
651
652 bool si476x_core_is_a_secondary_tuner(struct si476x_core *core)
653 {
654 return si476x_core_has_diversity(core) &&
655 (core->diversity_mode == SI476X_PHDIV_SECONDARY_ANTENNA ||
656 core->diversity_mode == SI476X_PHDIV_SECONDARY_COMBINING);
657 }
658 EXPORT_SYMBOL_GPL(si476x_core_is_a_secondary_tuner);
659
660 bool si476x_core_is_a_primary_tuner(struct si476x_core *core)
661 {
662 return si476x_core_has_diversity(core) &&
663 (core->diversity_mode == SI476X_PHDIV_PRIMARY_ANTENNA ||
664 core->diversity_mode == SI476X_PHDIV_PRIMARY_COMBINING);
665 }
666 EXPORT_SYMBOL_GPL(si476x_core_is_a_primary_tuner);
667
668 bool si476x_core_is_in_am_receiver_mode(struct si476x_core *core)
669 {
670 return si476x_core_has_am(core) &&
671 (core->power_up_parameters.func == SI476X_FUNC_AM_RECEIVER);
672 }
673 EXPORT_SYMBOL_GPL(si476x_core_is_in_am_receiver_mode);
674
675 bool si476x_core_is_powered_up(struct si476x_core *core)
676 {
677 return core->power_state == SI476X_POWER_UP_FULL;
678 }
679 EXPORT_SYMBOL_GPL(si476x_core_is_powered_up);
680
681 static int si476x_core_probe(struct i2c_client *client,
682 const struct i2c_device_id *id)
683 {
684 int rval;
685 struct si476x_core *core;
686 struct si476x_platform_data *pdata;
687 struct mfd_cell *cell;
688 int cell_num;
689
690 core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
691 if (!core)
692 return -ENOMEM;
693
694 core->client = client;
695
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",
701 rval);
702 return rval;
703 }
704
705 i2c_set_clientdata(client, core);
706
707 atomic_set(&core->is_alive, 0);
708 core->power_state = SI476X_POWER_DOWN;
709
710 pdata = dev_get_platdata(&client->dev);
711 if (pdata) {
712 memcpy(&core->power_up_parameters,
713 &pdata->power_up_parameters,
714 sizeof(core->power_up_parameters));
715
716 core->gpio_reset = -1;
717 if (gpio_is_valid(pdata->gpio_reset)) {
718 rval = gpio_request(pdata->gpio_reset, "si476x reset");
719 if (rval) {
720 dev_err(&client->dev,
721 "Failed to request gpio: %d\n", rval);
722 return rval;
723 }
724 core->gpio_reset = pdata->gpio_reset;
725 gpio_direction_output(core->gpio_reset, 0);
726 }
727
728 core->diversity_mode = pdata->diversity_mode;
729 memcpy(&core->pinmux, &pdata->pinmux,
730 sizeof(struct si476x_pinmux));
731 } else {
732 dev_err(&client->dev, "No platform data provided\n");
733 return -EINVAL;
734 }
735
736 core->supplies[0].supply = "vd";
737 core->supplies[1].supply = "va";
738 core->supplies[2].supply = "vio1";
739 core->supplies[3].supply = "vio2";
740
741 rval = devm_regulator_bulk_get(&client->dev,
742 ARRAY_SIZE(core->supplies),
743 core->supplies);
744 if (rval) {
745 dev_err(&client->dev, "Failed to get all of the regulators\n");
746 goto free_gpio;
747 }
748
749 mutex_init(&core->cmd_lock);
750 init_waitqueue_head(&core->command);
751 init_waitqueue_head(&core->tuning);
752
753 rval = kfifo_alloc(&core->rds_fifo,
754 SI476X_DRIVER_RDS_FIFO_DEPTH *
755 sizeof(struct v4l2_rds_data),
756 GFP_KERNEL);
757 if (rval) {
758 dev_err(&client->dev, "Could not allocate the FIFO\n");
759 goto free_gpio;
760 }
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);
764
765 if (client->irq) {
766 rval = devm_request_threaded_irq(&client->dev,
767 client->irq, NULL,
768 si476x_core_interrupt,
769 IRQF_TRIGGER_FALLING |
770 IRQF_ONESHOT,
771 client->name, core);
772 if (rval < 0) {
773 dev_err(&client->dev, "Could not request IRQ %d\n",
774 client->irq);
775 goto free_kfifo;
776 }
777 disable_irq(client->irq);
778 dev_dbg(&client->dev, "IRQ requested.\n");
779
780 core->rds_fifo_depth = 20;
781 } else {
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");
786
787 core->rds_fifo_depth = 5;
788 }
789
790 core->chip_id = id->driver_data;
791
792 rval = si476x_core_get_revision_info(core);
793 if (rval < 0) {
794 rval = -ENODEV;
795 goto free_kfifo;
796 }
797
798 cell_num = 0;
799
800 cell = &core->cells[SI476X_RADIO_CELL];
801 cell->name = "si476x-radio";
802 cell_num++;
803
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";
813 cell_num++;
814 }
815 #endif
816 rval = mfd_add_devices(&client->dev,
817 (client->adapter->nr << 8) + client->addr,
818 core->cells, cell_num,
819 NULL, 0, NULL);
820 if (!rval)
821 return 0;
822
823 free_kfifo:
824 kfifo_free(&core->rds_fifo);
825
826 free_gpio:
827 if (gpio_is_valid(core->gpio_reset))
828 gpio_free(core->gpio_reset);
829
830 return rval;
831 }
832
833 static int si476x_core_remove(struct i2c_client *client)
834 {
835 struct si476x_core *core = i2c_get_clientdata(client);
836
837 si476x_core_pronounce_dead(core);
838 mfd_remove_devices(&client->dev);
839
840 if (client->irq)
841 disable_irq(client->irq);
842 else
843 cancel_delayed_work_sync(&core->status_monitor);
844
845 kfifo_free(&core->rds_fifo);
846
847 if (gpio_is_valid(core->gpio_reset))
848 gpio_free(core->gpio_reset);
849
850 return 0;
851 }
852
853
854 static const struct i2c_device_id si476x_id[] = {
855 { "si4761", SI476X_CHIP_SI4761 },
856 { "si4764", SI476X_CHIP_SI4764 },
857 { "si4768", SI476X_CHIP_SI4768 },
858 { },
859 };
860 MODULE_DEVICE_TABLE(i2c, si476x_id);
861
862 static struct i2c_driver si476x_core_driver = {
863 .driver = {
864 .name = "si476x-core",
865 },
866 .probe = si476x_core_probe,
867 .remove = si476x_core_remove,
868 .id_table = si476x_id,
869 };
870 module_i2c_driver(si476x_core_driver);
871
872
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");