Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / media / radio / wl128x / fmdrv_common.c
blob3f9e6df7d837ac27d060a1d7a5413de92273f57f
1 /*
2 * FM Driver for Connectivity chip of Texas Instruments.
4 * This sub-module of FM driver is common for FM RX and TX
5 * functionality. This module is responsible for:
6 * 1) Forming group of Channel-8 commands to perform particular
7 * functionality (eg., frequency set require more than
8 * one Channel-8 command to be sent to the chip).
9 * 2) Sending each Channel-8 command to the chip and reading
10 * response back over Shared Transport.
11 * 3) Managing TX and RX Queues and Tasklets.
12 * 4) Handling FM Interrupt packet and taking appropriate action.
13 * 5) Loading FM firmware to the chip (common, FM TX, and FM RX
14 * firmware files based on mode selection)
16 * Copyright (C) 2011 Texas Instruments
17 * Author: Raja Mani <raja_mani@ti.com>
18 * Author: Manjunatha Halli <manjunatha_halli@ti.com>
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2 as
22 * published by the Free Software Foundation.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <linux/module.h>
36 #include <linux/firmware.h>
37 #include <linux/delay.h>
38 #include "fmdrv.h"
39 #include "fmdrv_v4l2.h"
40 #include "fmdrv_common.h"
41 #include <linux/ti_wilink_st.h>
42 #include "fmdrv_rx.h"
43 #include "fmdrv_tx.h"
45 /* Region info */
46 static struct region_info region_configs[] = {
47 /* Europe/US */
49 .chanl_space = FM_CHANNEL_SPACING_200KHZ * FM_FREQ_MUL,
50 .bot_freq = 87500, /* 87.5 MHz */
51 .top_freq = 108000, /* 108 MHz */
52 .fm_band = 0,
54 /* Japan */
56 .chanl_space = FM_CHANNEL_SPACING_200KHZ * FM_FREQ_MUL,
57 .bot_freq = 76000, /* 76 MHz */
58 .top_freq = 90000, /* 90 MHz */
59 .fm_band = 1,
63 /* Band selection */
64 static u8 default_radio_region; /* Europe/US */
65 module_param(default_radio_region, byte, 0);
66 MODULE_PARM_DESC(default_radio_region, "Region: 0=Europe/US, 1=Japan");
68 /* RDS buffer blocks */
69 static u32 default_rds_buf = 300;
70 module_param(default_rds_buf, uint, 0444);
71 MODULE_PARM_DESC(default_rds_buf, "RDS buffer entries");
73 /* Radio Nr */
74 static u32 radio_nr = -1;
75 module_param(radio_nr, int, 0444);
76 MODULE_PARM_DESC(radio_nr, "Radio Nr");
78 /* FM irq handlers forward declaration */
79 static void fm_irq_send_flag_getcmd(struct fmdev *);
80 static void fm_irq_handle_flag_getcmd_resp(struct fmdev *);
81 static void fm_irq_handle_hw_malfunction(struct fmdev *);
82 static void fm_irq_handle_rds_start(struct fmdev *);
83 static void fm_irq_send_rdsdata_getcmd(struct fmdev *);
84 static void fm_irq_handle_rdsdata_getcmd_resp(struct fmdev *);
85 static void fm_irq_handle_rds_finish(struct fmdev *);
86 static void fm_irq_handle_tune_op_ended(struct fmdev *);
87 static void fm_irq_handle_power_enb(struct fmdev *);
88 static void fm_irq_handle_low_rssi_start(struct fmdev *);
89 static void fm_irq_afjump_set_pi(struct fmdev *);
90 static void fm_irq_handle_set_pi_resp(struct fmdev *);
91 static void fm_irq_afjump_set_pimask(struct fmdev *);
92 static void fm_irq_handle_set_pimask_resp(struct fmdev *);
93 static void fm_irq_afjump_setfreq(struct fmdev *);
94 static void fm_irq_handle_setfreq_resp(struct fmdev *);
95 static void fm_irq_afjump_enableint(struct fmdev *);
96 static void fm_irq_afjump_enableint_resp(struct fmdev *);
97 static void fm_irq_start_afjump(struct fmdev *);
98 static void fm_irq_handle_start_afjump_resp(struct fmdev *);
99 static void fm_irq_afjump_rd_freq(struct fmdev *);
100 static void fm_irq_afjump_rd_freq_resp(struct fmdev *);
101 static void fm_irq_handle_low_rssi_finish(struct fmdev *);
102 static void fm_irq_send_intmsk_cmd(struct fmdev *);
103 static void fm_irq_handle_intmsk_cmd_resp(struct fmdev *);
106 * When FM common module receives interrupt packet, following handlers
107 * will be executed one after another to service the interrupt(s)
109 enum fmc_irq_handler_index {
110 FM_SEND_FLAG_GETCMD_IDX,
111 FM_HANDLE_FLAG_GETCMD_RESP_IDX,
113 /* HW malfunction irq handler */
114 FM_HW_MAL_FUNC_IDX,
116 /* RDS threshold reached irq handler */
117 FM_RDS_START_IDX,
118 FM_RDS_SEND_RDS_GETCMD_IDX,
119 FM_RDS_HANDLE_RDS_GETCMD_RESP_IDX,
120 FM_RDS_FINISH_IDX,
122 /* Tune operation ended irq handler */
123 FM_HW_TUNE_OP_ENDED_IDX,
125 /* TX power enable irq handler */
126 FM_HW_POWER_ENB_IDX,
128 /* Low RSSI irq handler */
129 FM_LOW_RSSI_START_IDX,
130 FM_AF_JUMP_SETPI_IDX,
131 FM_AF_JUMP_HANDLE_SETPI_RESP_IDX,
132 FM_AF_JUMP_SETPI_MASK_IDX,
133 FM_AF_JUMP_HANDLE_SETPI_MASK_RESP_IDX,
134 FM_AF_JUMP_SET_AF_FREQ_IDX,
135 FM_AF_JUMP_HANDLE_SET_AFFREQ_RESP_IDX,
136 FM_AF_JUMP_ENABLE_INT_IDX,
137 FM_AF_JUMP_ENABLE_INT_RESP_IDX,
138 FM_AF_JUMP_START_AFJUMP_IDX,
139 FM_AF_JUMP_HANDLE_START_AFJUMP_RESP_IDX,
140 FM_AF_JUMP_RD_FREQ_IDX,
141 FM_AF_JUMP_RD_FREQ_RESP_IDX,
142 FM_LOW_RSSI_FINISH_IDX,
144 /* Interrupt process post action */
145 FM_SEND_INTMSK_CMD_IDX,
146 FM_HANDLE_INTMSK_CMD_RESP_IDX,
149 /* FM interrupt handler table */
150 static int_handler_prototype int_handler_table[] = {
151 fm_irq_send_flag_getcmd,
152 fm_irq_handle_flag_getcmd_resp,
153 fm_irq_handle_hw_malfunction,
154 fm_irq_handle_rds_start, /* RDS threshold reached irq handler */
155 fm_irq_send_rdsdata_getcmd,
156 fm_irq_handle_rdsdata_getcmd_resp,
157 fm_irq_handle_rds_finish,
158 fm_irq_handle_tune_op_ended,
159 fm_irq_handle_power_enb, /* TX power enable irq handler */
160 fm_irq_handle_low_rssi_start,
161 fm_irq_afjump_set_pi,
162 fm_irq_handle_set_pi_resp,
163 fm_irq_afjump_set_pimask,
164 fm_irq_handle_set_pimask_resp,
165 fm_irq_afjump_setfreq,
166 fm_irq_handle_setfreq_resp,
167 fm_irq_afjump_enableint,
168 fm_irq_afjump_enableint_resp,
169 fm_irq_start_afjump,
170 fm_irq_handle_start_afjump_resp,
171 fm_irq_afjump_rd_freq,
172 fm_irq_afjump_rd_freq_resp,
173 fm_irq_handle_low_rssi_finish,
174 fm_irq_send_intmsk_cmd, /* Interrupt process post action */
175 fm_irq_handle_intmsk_cmd_resp
178 static long (*g_st_write) (struct sk_buff *skb);
179 static struct completion wait_for_fmdrv_reg_comp;
181 static inline void fm_irq_call(struct fmdev *fmdev)
183 fmdev->irq_info.handlers[fmdev->irq_info.stage](fmdev);
186 /* Continue next function in interrupt handler table */
187 static inline void fm_irq_call_stage(struct fmdev *fmdev, u8 stage)
189 fmdev->irq_info.stage = stage;
190 fm_irq_call(fmdev);
193 static inline void fm_irq_timeout_stage(struct fmdev *fmdev, u8 stage)
195 fmdev->irq_info.stage = stage;
196 mod_timer(&fmdev->irq_info.timer, jiffies + FM_DRV_TX_TIMEOUT);
199 #ifdef FM_DUMP_TXRX_PKT
200 /* To dump outgoing FM Channel-8 packets */
201 inline void dump_tx_skb_data(struct sk_buff *skb)
203 int len, len_org;
204 u8 index;
205 struct fm_cmd_msg_hdr *cmd_hdr;
207 cmd_hdr = (struct fm_cmd_msg_hdr *)skb->data;
208 printk(KERN_INFO "<<%shdr:%02x len:%02x opcode:%02x type:%s dlen:%02x",
209 fm_cb(skb)->completion ? " " : "*", cmd_hdr->hdr,
210 cmd_hdr->len, cmd_hdr->op,
211 cmd_hdr->rd_wr ? "RD" : "WR", cmd_hdr->dlen);
213 len_org = skb->len - FM_CMD_MSG_HDR_SIZE;
214 if (len_org > 0) {
215 printk("\n data(%d): ", cmd_hdr->dlen);
216 len = min(len_org, 14);
217 for (index = 0; index < len; index++)
218 printk("%x ",
219 skb->data[FM_CMD_MSG_HDR_SIZE + index]);
220 printk("%s", (len_org > 14) ? ".." : "");
222 printk("\n");
225 /* To dump incoming FM Channel-8 packets */
226 inline void dump_rx_skb_data(struct sk_buff *skb)
228 int len, len_org;
229 u8 index;
230 struct fm_event_msg_hdr *evt_hdr;
232 evt_hdr = (struct fm_event_msg_hdr *)skb->data;
233 printk(KERN_INFO ">> hdr:%02x len:%02x sts:%02x numhci:%02x "
234 "opcode:%02x type:%s dlen:%02x", evt_hdr->hdr, evt_hdr->len,
235 evt_hdr->status, evt_hdr->num_fm_hci_cmds, evt_hdr->op,
236 (evt_hdr->rd_wr) ? "RD" : "WR", evt_hdr->dlen);
238 len_org = skb->len - FM_EVT_MSG_HDR_SIZE;
239 if (len_org > 0) {
240 printk("\n data(%d): ", evt_hdr->dlen);
241 len = min(len_org, 14);
242 for (index = 0; index < len; index++)
243 printk("%x ",
244 skb->data[FM_EVT_MSG_HDR_SIZE + index]);
245 printk("%s", (len_org > 14) ? ".." : "");
247 printk("\n");
249 #endif
251 void fmc_update_region_info(struct fmdev *fmdev, u8 region_to_set)
253 fmdev->rx.region = region_configs[region_to_set];
257 * FM common sub-module will schedule this tasklet whenever it receives
258 * FM packet from ST driver.
260 static void recv_tasklet(unsigned long arg)
262 struct fmdev *fmdev;
263 struct fm_irq *irq_info;
264 struct fm_event_msg_hdr *evt_hdr;
265 struct sk_buff *skb;
266 u8 num_fm_hci_cmds;
267 unsigned long flags;
269 fmdev = (struct fmdev *)arg;
270 irq_info = &fmdev->irq_info;
271 /* Process all packets in the RX queue */
272 while ((skb = skb_dequeue(&fmdev->rx_q))) {
273 if (skb->len < sizeof(struct fm_event_msg_hdr)) {
274 fmerr("skb(%p) has only %d bytes, "
275 "at least need %zu bytes to decode\n", skb,
276 skb->len, sizeof(struct fm_event_msg_hdr));
277 kfree_skb(skb);
278 continue;
281 evt_hdr = (void *)skb->data;
282 num_fm_hci_cmds = evt_hdr->num_fm_hci_cmds;
284 /* FM interrupt packet? */
285 if (evt_hdr->op == FM_INTERRUPT) {
286 /* FM interrupt handler started already? */
287 if (!test_bit(FM_INTTASK_RUNNING, &fmdev->flag)) {
288 set_bit(FM_INTTASK_RUNNING, &fmdev->flag);
289 if (irq_info->stage != 0) {
290 fmerr("Inval stage resetting to zero\n");
291 irq_info->stage = 0;
295 * Execute first function in interrupt handler
296 * table.
298 irq_info->handlers[irq_info->stage](fmdev);
299 } else {
300 set_bit(FM_INTTASK_SCHEDULE_PENDING, &fmdev->flag);
302 kfree_skb(skb);
304 /* Anyone waiting for this with completion handler? */
305 else if (evt_hdr->op == fmdev->pre_op && fmdev->resp_comp != NULL) {
307 spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
308 fmdev->resp_skb = skb;
309 spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
310 complete(fmdev->resp_comp);
312 fmdev->resp_comp = NULL;
313 atomic_set(&fmdev->tx_cnt, 1);
315 /* Is this for interrupt handler? */
316 else if (evt_hdr->op == fmdev->pre_op && fmdev->resp_comp == NULL) {
317 if (fmdev->resp_skb != NULL)
318 fmerr("Response SKB ptr not NULL\n");
320 spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
321 fmdev->resp_skb = skb;
322 spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
324 /* Execute interrupt handler where state index points */
325 irq_info->handlers[irq_info->stage](fmdev);
327 kfree_skb(skb);
328 atomic_set(&fmdev->tx_cnt, 1);
329 } else {
330 fmerr("Nobody claimed SKB(%p),purging\n", skb);
334 * Check flow control field. If Num_FM_HCI_Commands field is
335 * not zero, schedule FM TX tasklet.
337 if (num_fm_hci_cmds && atomic_read(&fmdev->tx_cnt))
338 if (!skb_queue_empty(&fmdev->tx_q))
339 tasklet_schedule(&fmdev->tx_task);
343 /* FM send tasklet: is scheduled when FM packet has to be sent to chip */
344 static void send_tasklet(unsigned long arg)
346 struct fmdev *fmdev;
347 struct sk_buff *skb;
348 int len;
350 fmdev = (struct fmdev *)arg;
352 if (!atomic_read(&fmdev->tx_cnt))
353 return;
355 /* Check, is there any timeout happened to last transmitted packet */
356 if ((jiffies - fmdev->last_tx_jiffies) > FM_DRV_TX_TIMEOUT) {
357 fmerr("TX timeout occurred\n");
358 atomic_set(&fmdev->tx_cnt, 1);
361 /* Send queued FM TX packets */
362 skb = skb_dequeue(&fmdev->tx_q);
363 if (!skb)
364 return;
366 atomic_dec(&fmdev->tx_cnt);
367 fmdev->pre_op = fm_cb(skb)->fm_op;
369 if (fmdev->resp_comp != NULL)
370 fmerr("Response completion handler is not NULL\n");
372 fmdev->resp_comp = fm_cb(skb)->completion;
374 /* Write FM packet to ST driver */
375 len = g_st_write(skb);
376 if (len < 0) {
377 kfree_skb(skb);
378 fmdev->resp_comp = NULL;
379 fmerr("TX tasklet failed to send skb(%p)\n", skb);
380 atomic_set(&fmdev->tx_cnt, 1);
381 } else {
382 fmdev->last_tx_jiffies = jiffies;
387 * Queues FM Channel-8 packet to FM TX queue and schedules FM TX tasklet for
388 * transmission
390 static int fm_send_cmd(struct fmdev *fmdev, u8 fm_op, u16 type, void *payload,
391 int payload_len, struct completion *wait_completion)
393 struct sk_buff *skb;
394 struct fm_cmd_msg_hdr *hdr;
395 int size;
397 if (fm_op >= FM_INTERRUPT) {
398 fmerr("Invalid fm opcode - %d\n", fm_op);
399 return -EINVAL;
401 if (test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag) && payload == NULL) {
402 fmerr("Payload data is NULL during fw download\n");
403 return -EINVAL;
405 if (!test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag))
406 size =
407 FM_CMD_MSG_HDR_SIZE + ((payload == NULL) ? 0 : payload_len);
408 else
409 size = payload_len;
411 skb = alloc_skb(size, GFP_ATOMIC);
412 if (!skb) {
413 fmerr("No memory to create new SKB\n");
414 return -ENOMEM;
417 * Don't fill FM header info for the commands which come from
418 * FM firmware file.
420 if (!test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag) ||
421 test_bit(FM_INTTASK_RUNNING, &fmdev->flag)) {
422 /* Fill command header info */
423 hdr = (struct fm_cmd_msg_hdr *)skb_put(skb, FM_CMD_MSG_HDR_SIZE);
424 hdr->hdr = FM_PKT_LOGICAL_CHAN_NUMBER; /* 0x08 */
426 /* 3 (fm_opcode,rd_wr,dlen) + payload len) */
427 hdr->len = ((payload == NULL) ? 0 : payload_len) + 3;
429 /* FM opcode */
430 hdr->op = fm_op;
432 /* read/write type */
433 hdr->rd_wr = type;
434 hdr->dlen = payload_len;
435 fm_cb(skb)->fm_op = fm_op;
438 * If firmware download has finished and the command is
439 * not a read command then payload is != NULL - a write
440 * command with u16 payload - convert to be16
442 if (payload != NULL)
443 *(__be16 *)payload = cpu_to_be16(*(u16 *)payload);
445 } else if (payload != NULL) {
446 fm_cb(skb)->fm_op = *((u8 *)payload + 2);
448 if (payload != NULL)
449 memcpy(skb_put(skb, payload_len), payload, payload_len);
451 fm_cb(skb)->completion = wait_completion;
452 skb_queue_tail(&fmdev->tx_q, skb);
453 tasklet_schedule(&fmdev->tx_task);
455 return 0;
458 /* Sends FM Channel-8 command to the chip and waits for the response */
459 int fmc_send_cmd(struct fmdev *fmdev, u8 fm_op, u16 type, void *payload,
460 unsigned int payload_len, void *response, int *response_len)
462 struct sk_buff *skb;
463 struct fm_event_msg_hdr *evt_hdr;
464 unsigned long flags;
465 int ret;
467 init_completion(&fmdev->maintask_comp);
468 ret = fm_send_cmd(fmdev, fm_op, type, payload, payload_len,
469 &fmdev->maintask_comp);
470 if (ret)
471 return ret;
473 if (!wait_for_completion_timeout(&fmdev->maintask_comp,
474 FM_DRV_TX_TIMEOUT)) {
475 fmerr("Timeout(%d sec),didn't get reg"
476 "completion signal from RX tasklet\n",
477 jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
478 return -ETIMEDOUT;
480 if (!fmdev->resp_skb) {
481 fmerr("Response SKB is missing\n");
482 return -EFAULT;
484 spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
485 skb = fmdev->resp_skb;
486 fmdev->resp_skb = NULL;
487 spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
489 evt_hdr = (void *)skb->data;
490 if (evt_hdr->status != 0) {
491 fmerr("Received event pkt status(%d) is not zero\n",
492 evt_hdr->status);
493 kfree_skb(skb);
494 return -EIO;
496 /* Send response data to caller */
497 if (response != NULL && response_len != NULL && evt_hdr->dlen) {
498 /* Skip header info and copy only response data */
499 skb_pull(skb, sizeof(struct fm_event_msg_hdr));
500 memcpy(response, skb->data, evt_hdr->dlen);
501 *response_len = evt_hdr->dlen;
502 } else if (response_len != NULL && evt_hdr->dlen == 0) {
503 *response_len = 0;
505 kfree_skb(skb);
507 return 0;
510 /* --- Helper functions used in FM interrupt handlers ---*/
511 static inline int check_cmdresp_status(struct fmdev *fmdev,
512 struct sk_buff **skb)
514 struct fm_event_msg_hdr *fm_evt_hdr;
515 unsigned long flags;
517 del_timer(&fmdev->irq_info.timer);
519 spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
520 *skb = fmdev->resp_skb;
521 fmdev->resp_skb = NULL;
522 spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
524 fm_evt_hdr = (void *)(*skb)->data;
525 if (fm_evt_hdr->status != 0) {
526 fmerr("irq: opcode %x response status is not zero "
527 "Initiating irq recovery process\n",
528 fm_evt_hdr->op);
530 mod_timer(&fmdev->irq_info.timer, jiffies + FM_DRV_TX_TIMEOUT);
531 return -1;
534 return 0;
537 static inline void fm_irq_common_cmd_resp_helper(struct fmdev *fmdev, u8 stage)
539 struct sk_buff *skb;
541 if (!check_cmdresp_status(fmdev, &skb))
542 fm_irq_call_stage(fmdev, stage);
546 * Interrupt process timeout handler.
547 * One of the irq handler did not get proper response from the chip. So take
548 * recovery action here. FM interrupts are disabled in the beginning of
549 * interrupt process. Therefore reset stage index to re-enable default
550 * interrupts. So that next interrupt will be processed as usual.
552 static void int_timeout_handler(unsigned long data)
554 struct fmdev *fmdev;
555 struct fm_irq *fmirq;
557 fmdbg("irq: timeout,trying to re-enable fm interrupts\n");
558 fmdev = (struct fmdev *)data;
559 fmirq = &fmdev->irq_info;
560 fmirq->retry++;
562 if (fmirq->retry > FM_IRQ_TIMEOUT_RETRY_MAX) {
563 /* Stop recovery action (interrupt reenable process) and
564 * reset stage index & retry count values */
565 fmirq->stage = 0;
566 fmirq->retry = 0;
567 fmerr("Recovery action failed during"
568 "irq processing, max retry reached\n");
569 return;
571 fm_irq_call_stage(fmdev, FM_SEND_INTMSK_CMD_IDX);
574 /* --------- FM interrupt handlers ------------*/
575 static void fm_irq_send_flag_getcmd(struct fmdev *fmdev)
577 u16 flag;
579 /* Send FLAG_GET command , to know the source of interrupt */
580 if (!fm_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, sizeof(flag), NULL))
581 fm_irq_timeout_stage(fmdev, FM_HANDLE_FLAG_GETCMD_RESP_IDX);
584 static void fm_irq_handle_flag_getcmd_resp(struct fmdev *fmdev)
586 struct sk_buff *skb;
587 struct fm_event_msg_hdr *fm_evt_hdr;
589 if (check_cmdresp_status(fmdev, &skb))
590 return;
592 fm_evt_hdr = (void *)skb->data;
594 /* Skip header info and copy only response data */
595 skb_pull(skb, sizeof(struct fm_event_msg_hdr));
596 memcpy(&fmdev->irq_info.flag, skb->data, fm_evt_hdr->dlen);
598 fmdev->irq_info.flag = be16_to_cpu((__force __be16)fmdev->irq_info.flag);
599 fmdbg("irq: flag register(0x%x)\n", fmdev->irq_info.flag);
601 /* Continue next function in interrupt handler table */
602 fm_irq_call_stage(fmdev, FM_HW_MAL_FUNC_IDX);
605 static void fm_irq_handle_hw_malfunction(struct fmdev *fmdev)
607 if (fmdev->irq_info.flag & FM_MAL_EVENT & fmdev->irq_info.mask)
608 fmerr("irq: HW MAL int received - do nothing\n");
610 /* Continue next function in interrupt handler table */
611 fm_irq_call_stage(fmdev, FM_RDS_START_IDX);
614 static void fm_irq_handle_rds_start(struct fmdev *fmdev)
616 if (fmdev->irq_info.flag & FM_RDS_EVENT & fmdev->irq_info.mask) {
617 fmdbg("irq: rds threshold reached\n");
618 fmdev->irq_info.stage = FM_RDS_SEND_RDS_GETCMD_IDX;
619 } else {
620 /* Continue next function in interrupt handler table */
621 fmdev->irq_info.stage = FM_HW_TUNE_OP_ENDED_IDX;
624 fm_irq_call(fmdev);
627 static void fm_irq_send_rdsdata_getcmd(struct fmdev *fmdev)
629 /* Send the command to read RDS data from the chip */
630 if (!fm_send_cmd(fmdev, RDS_DATA_GET, REG_RD, NULL,
631 (FM_RX_RDS_FIFO_THRESHOLD * 3), NULL))
632 fm_irq_timeout_stage(fmdev, FM_RDS_HANDLE_RDS_GETCMD_RESP_IDX);
635 /* Keeps track of current RX channel AF (Alternate Frequency) */
636 static void fm_rx_update_af_cache(struct fmdev *fmdev, u8 af)
638 struct tuned_station_info *stat_info = &fmdev->rx.stat_info;
639 u8 reg_idx = fmdev->rx.region.fm_band;
640 u8 index;
641 u32 freq;
643 /* First AF indicates the number of AF follows. Reset the list */
644 if ((af >= FM_RDS_1_AF_FOLLOWS) && (af <= FM_RDS_25_AF_FOLLOWS)) {
645 fmdev->rx.stat_info.af_list_max = (af - FM_RDS_1_AF_FOLLOWS + 1);
646 fmdev->rx.stat_info.afcache_size = 0;
647 fmdbg("No of expected AF : %d\n", fmdev->rx.stat_info.af_list_max);
648 return;
651 if (af < FM_RDS_MIN_AF)
652 return;
653 if (reg_idx == FM_BAND_EUROPE_US && af > FM_RDS_MAX_AF)
654 return;
655 if (reg_idx == FM_BAND_JAPAN && af > FM_RDS_MAX_AF_JAPAN)
656 return;
658 freq = fmdev->rx.region.bot_freq + (af * 100);
659 if (freq == fmdev->rx.freq) {
660 fmdbg("Current freq(%d) is matching with received AF(%d)\n",
661 fmdev->rx.freq, freq);
662 return;
664 /* Do check in AF cache */
665 for (index = 0; index < stat_info->afcache_size; index++) {
666 if (stat_info->af_cache[index] == freq)
667 break;
669 /* Reached the limit of the list - ignore the next AF */
670 if (index == stat_info->af_list_max) {
671 fmdbg("AF cache is full\n");
672 return;
675 * If we reached the end of the list then this AF is not
676 * in the list - add it.
678 if (index == stat_info->afcache_size) {
679 fmdbg("Storing AF %d to cache index %d\n", freq, index);
680 stat_info->af_cache[index] = freq;
681 stat_info->afcache_size++;
686 * Converts RDS buffer data from big endian format
687 * to little endian format.
689 static void fm_rdsparse_swapbytes(struct fmdev *fmdev,
690 struct fm_rdsdata_format *rds_format)
692 u8 index = 0;
693 u8 *rds_buff;
696 * Since in Orca the 2 RDS Data bytes are in little endian and
697 * in Dolphin they are in big endian, the parsing of the RDS data
698 * is chip dependent
700 if (fmdev->asci_id != 0x6350) {
701 rds_buff = &rds_format->data.groupdatabuff.buff[0];
702 while (index + 1 < FM_RX_RDS_INFO_FIELD_MAX) {
703 swap(rds_buff[index], rds_buff[index + 1]);
704 index += 2;
709 static void fm_irq_handle_rdsdata_getcmd_resp(struct fmdev *fmdev)
711 struct sk_buff *skb;
712 struct fm_rdsdata_format rds_fmt;
713 struct fm_rds *rds = &fmdev->rx.rds;
714 unsigned long group_idx, flags;
715 u8 *rds_data, meta_data, tmpbuf[FM_RDS_BLK_SIZE];
716 u8 type, blk_idx;
717 u16 cur_picode;
718 u32 rds_len;
720 if (check_cmdresp_status(fmdev, &skb))
721 return;
723 /* Skip header info */
724 skb_pull(skb, sizeof(struct fm_event_msg_hdr));
725 rds_data = skb->data;
726 rds_len = skb->len;
728 /* Parse the RDS data */
729 while (rds_len >= FM_RDS_BLK_SIZE) {
730 meta_data = rds_data[2];
731 /* Get the type: 0=A, 1=B, 2=C, 3=C', 4=D, 5=E */
732 type = (meta_data & 0x07);
734 /* Transform the blk type into index sequence (0, 1, 2, 3, 4) */
735 blk_idx = (type <= FM_RDS_BLOCK_C ? type : (type - 1));
736 fmdbg("Block index:%d(%s)\n", blk_idx,
737 (meta_data & FM_RDS_STATUS_ERR_MASK) ? "Bad" : "Ok");
739 if ((meta_data & FM_RDS_STATUS_ERR_MASK) != 0)
740 break;
742 if (blk_idx > FM_RDS_BLK_IDX_D) {
743 fmdbg("Block sequence mismatch\n");
744 rds->last_blk_idx = -1;
745 break;
748 /* Skip checkword (control) byte and copy only data byte */
749 memcpy(&rds_fmt.data.groupdatabuff.
750 buff[blk_idx * (FM_RDS_BLK_SIZE - 1)],
751 rds_data, (FM_RDS_BLK_SIZE - 1));
753 rds->last_blk_idx = blk_idx;
755 /* If completed a whole group then handle it */
756 if (blk_idx == FM_RDS_BLK_IDX_D) {
757 fmdbg("Good block received\n");
758 fm_rdsparse_swapbytes(fmdev, &rds_fmt);
761 * Extract PI code and store in local cache.
762 * We need this during AF switch processing.
764 cur_picode = be16_to_cpu((__force __be16)rds_fmt.data.groupgeneral.pidata);
765 if (fmdev->rx.stat_info.picode != cur_picode)
766 fmdev->rx.stat_info.picode = cur_picode;
768 fmdbg("picode:%d\n", cur_picode);
770 group_idx = (rds_fmt.data.groupgeneral.blk_b[0] >> 3);
771 fmdbg("(fmdrv):Group:%ld%s\n", group_idx/2,
772 (group_idx % 2) ? "B" : "A");
774 group_idx = 1 << (rds_fmt.data.groupgeneral.blk_b[0] >> 3);
775 if (group_idx == FM_RDS_GROUP_TYPE_MASK_0A) {
776 fm_rx_update_af_cache(fmdev, rds_fmt.data.group0A.af[0]);
777 fm_rx_update_af_cache(fmdev, rds_fmt.data.group0A.af[1]);
780 rds_len -= FM_RDS_BLK_SIZE;
781 rds_data += FM_RDS_BLK_SIZE;
784 /* Copy raw rds data to internal rds buffer */
785 rds_data = skb->data;
786 rds_len = skb->len;
788 spin_lock_irqsave(&fmdev->rds_buff_lock, flags);
789 while (rds_len > 0) {
791 * Fill RDS buffer as per V4L2 specification.
792 * Store control byte
794 type = (rds_data[2] & 0x07);
795 blk_idx = (type <= FM_RDS_BLOCK_C ? type : (type - 1));
796 tmpbuf[2] = blk_idx; /* Offset name */
797 tmpbuf[2] |= blk_idx << 3; /* Received offset */
799 /* Store data byte */
800 tmpbuf[0] = rds_data[0];
801 tmpbuf[1] = rds_data[1];
803 memcpy(&rds->buff[rds->wr_idx], &tmpbuf, FM_RDS_BLK_SIZE);
804 rds->wr_idx = (rds->wr_idx + FM_RDS_BLK_SIZE) % rds->buf_size;
806 /* Check for overflow & start over */
807 if (rds->wr_idx == rds->rd_idx) {
808 fmdbg("RDS buffer overflow\n");
809 rds->wr_idx = 0;
810 rds->rd_idx = 0;
811 break;
813 rds_len -= FM_RDS_BLK_SIZE;
814 rds_data += FM_RDS_BLK_SIZE;
816 spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
818 /* Wakeup read queue */
819 if (rds->wr_idx != rds->rd_idx)
820 wake_up_interruptible(&rds->read_queue);
822 fm_irq_call_stage(fmdev, FM_RDS_FINISH_IDX);
825 static void fm_irq_handle_rds_finish(struct fmdev *fmdev)
827 fm_irq_call_stage(fmdev, FM_HW_TUNE_OP_ENDED_IDX);
830 static void fm_irq_handle_tune_op_ended(struct fmdev *fmdev)
832 if (fmdev->irq_info.flag & (FM_FR_EVENT | FM_BL_EVENT) & fmdev->
833 irq_info.mask) {
834 fmdbg("irq: tune ended/bandlimit reached\n");
835 if (test_and_clear_bit(FM_AF_SWITCH_INPROGRESS, &fmdev->flag)) {
836 fmdev->irq_info.stage = FM_AF_JUMP_RD_FREQ_IDX;
837 } else {
838 complete(&fmdev->maintask_comp);
839 fmdev->irq_info.stage = FM_HW_POWER_ENB_IDX;
841 } else
842 fmdev->irq_info.stage = FM_HW_POWER_ENB_IDX;
844 fm_irq_call(fmdev);
847 static void fm_irq_handle_power_enb(struct fmdev *fmdev)
849 if (fmdev->irq_info.flag & FM_POW_ENB_EVENT) {
850 fmdbg("irq: Power Enabled/Disabled\n");
851 complete(&fmdev->maintask_comp);
854 fm_irq_call_stage(fmdev, FM_LOW_RSSI_START_IDX);
857 static void fm_irq_handle_low_rssi_start(struct fmdev *fmdev)
859 if ((fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON) &&
860 (fmdev->irq_info.flag & FM_LEV_EVENT & fmdev->irq_info.mask) &&
861 (fmdev->rx.freq != FM_UNDEFINED_FREQ) &&
862 (fmdev->rx.stat_info.afcache_size != 0)) {
863 fmdbg("irq: rssi level has fallen below threshold level\n");
865 /* Disable further low RSSI interrupts */
866 fmdev->irq_info.mask &= ~FM_LEV_EVENT;
868 fmdev->rx.afjump_idx = 0;
869 fmdev->rx.freq_before_jump = fmdev->rx.freq;
870 fmdev->irq_info.stage = FM_AF_JUMP_SETPI_IDX;
871 } else {
872 /* Continue next function in interrupt handler table */
873 fmdev->irq_info.stage = FM_SEND_INTMSK_CMD_IDX;
876 fm_irq_call(fmdev);
879 static void fm_irq_afjump_set_pi(struct fmdev *fmdev)
881 u16 payload;
883 /* Set PI code - must be updated if the AF list is not empty */
884 payload = fmdev->rx.stat_info.picode;
885 if (!fm_send_cmd(fmdev, RDS_PI_SET, REG_WR, &payload, sizeof(payload), NULL))
886 fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SETPI_RESP_IDX);
889 static void fm_irq_handle_set_pi_resp(struct fmdev *fmdev)
891 fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_SETPI_MASK_IDX);
895 * Set PI mask.
896 * 0xFFFF = Enable PI code matching
897 * 0x0000 = Disable PI code matching
899 static void fm_irq_afjump_set_pimask(struct fmdev *fmdev)
901 u16 payload;
903 payload = 0x0000;
904 if (!fm_send_cmd(fmdev, RDS_PI_MASK_SET, REG_WR, &payload, sizeof(payload), NULL))
905 fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SETPI_MASK_RESP_IDX);
908 static void fm_irq_handle_set_pimask_resp(struct fmdev *fmdev)
910 fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_SET_AF_FREQ_IDX);
913 static void fm_irq_afjump_setfreq(struct fmdev *fmdev)
915 u16 frq_index;
916 u16 payload;
918 fmdbg("Swtich to %d KHz\n", fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx]);
919 frq_index = (fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx] -
920 fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
922 payload = frq_index;
923 if (!fm_send_cmd(fmdev, AF_FREQ_SET, REG_WR, &payload, sizeof(payload), NULL))
924 fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SET_AFFREQ_RESP_IDX);
927 static void fm_irq_handle_setfreq_resp(struct fmdev *fmdev)
929 fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_ENABLE_INT_IDX);
932 static void fm_irq_afjump_enableint(struct fmdev *fmdev)
934 u16 payload;
936 /* Enable FR (tuning operation ended) interrupt */
937 payload = FM_FR_EVENT;
938 if (!fm_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload, sizeof(payload), NULL))
939 fm_irq_timeout_stage(fmdev, FM_AF_JUMP_ENABLE_INT_RESP_IDX);
942 static void fm_irq_afjump_enableint_resp(struct fmdev *fmdev)
944 fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_START_AFJUMP_IDX);
947 static void fm_irq_start_afjump(struct fmdev *fmdev)
949 u16 payload;
951 payload = FM_TUNER_AF_JUMP_MODE;
952 if (!fm_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
953 sizeof(payload), NULL))
954 fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_START_AFJUMP_RESP_IDX);
957 static void fm_irq_handle_start_afjump_resp(struct fmdev *fmdev)
959 struct sk_buff *skb;
961 if (check_cmdresp_status(fmdev, &skb))
962 return;
964 fmdev->irq_info.stage = FM_SEND_FLAG_GETCMD_IDX;
965 set_bit(FM_AF_SWITCH_INPROGRESS, &fmdev->flag);
966 clear_bit(FM_INTTASK_RUNNING, &fmdev->flag);
969 static void fm_irq_afjump_rd_freq(struct fmdev *fmdev)
971 u16 payload;
973 if (!fm_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, sizeof(payload), NULL))
974 fm_irq_timeout_stage(fmdev, FM_AF_JUMP_RD_FREQ_RESP_IDX);
977 static void fm_irq_afjump_rd_freq_resp(struct fmdev *fmdev)
979 struct sk_buff *skb;
980 u16 read_freq;
981 u32 curr_freq, jumped_freq;
983 if (check_cmdresp_status(fmdev, &skb))
984 return;
986 /* Skip header info and copy only response data */
987 skb_pull(skb, sizeof(struct fm_event_msg_hdr));
988 memcpy(&read_freq, skb->data, sizeof(read_freq));
989 read_freq = be16_to_cpu((__force __be16)read_freq);
990 curr_freq = fmdev->rx.region.bot_freq + ((u32)read_freq * FM_FREQ_MUL);
992 jumped_freq = fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx];
994 /* If the frequency was changed the jump succeeded */
995 if ((curr_freq != fmdev->rx.freq_before_jump) && (curr_freq == jumped_freq)) {
996 fmdbg("Successfully switched to alternate freq %d\n", curr_freq);
997 fmdev->rx.freq = curr_freq;
998 fm_rx_reset_rds_cache(fmdev);
1000 /* AF feature is on, enable low level RSSI interrupt */
1001 if (fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
1002 fmdev->irq_info.mask |= FM_LEV_EVENT;
1004 fmdev->irq_info.stage = FM_LOW_RSSI_FINISH_IDX;
1005 } else { /* jump to the next freq in the AF list */
1006 fmdev->rx.afjump_idx++;
1008 /* If we reached the end of the list - stop searching */
1009 if (fmdev->rx.afjump_idx >= fmdev->rx.stat_info.afcache_size) {
1010 fmdbg("AF switch processing failed\n");
1011 fmdev->irq_info.stage = FM_LOW_RSSI_FINISH_IDX;
1012 } else { /* AF List is not over - try next one */
1014 fmdbg("Trying next freq in AF cache\n");
1015 fmdev->irq_info.stage = FM_AF_JUMP_SETPI_IDX;
1018 fm_irq_call(fmdev);
1021 static void fm_irq_handle_low_rssi_finish(struct fmdev *fmdev)
1023 fm_irq_call_stage(fmdev, FM_SEND_INTMSK_CMD_IDX);
1026 static void fm_irq_send_intmsk_cmd(struct fmdev *fmdev)
1028 u16 payload;
1030 /* Re-enable FM interrupts */
1031 payload = fmdev->irq_info.mask;
1033 if (!fm_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
1034 sizeof(payload), NULL))
1035 fm_irq_timeout_stage(fmdev, FM_HANDLE_INTMSK_CMD_RESP_IDX);
1038 static void fm_irq_handle_intmsk_cmd_resp(struct fmdev *fmdev)
1040 struct sk_buff *skb;
1042 if (check_cmdresp_status(fmdev, &skb))
1043 return;
1045 * This is last function in interrupt table to be executed.
1046 * So, reset stage index to 0.
1048 fmdev->irq_info.stage = FM_SEND_FLAG_GETCMD_IDX;
1050 /* Start processing any pending interrupt */
1051 if (test_and_clear_bit(FM_INTTASK_SCHEDULE_PENDING, &fmdev->flag))
1052 fmdev->irq_info.handlers[fmdev->irq_info.stage](fmdev);
1053 else
1054 clear_bit(FM_INTTASK_RUNNING, &fmdev->flag);
1057 /* Returns availability of RDS data in internel buffer */
1058 int fmc_is_rds_data_available(struct fmdev *fmdev, struct file *file,
1059 struct poll_table_struct *pts)
1061 poll_wait(file, &fmdev->rx.rds.read_queue, pts);
1062 if (fmdev->rx.rds.rd_idx != fmdev->rx.rds.wr_idx)
1063 return 0;
1065 return -EAGAIN;
1068 /* Copies RDS data from internal buffer to user buffer */
1069 int fmc_transfer_rds_from_internal_buff(struct fmdev *fmdev, struct file *file,
1070 u8 __user *buf, size_t count)
1072 u32 block_count;
1073 u8 tmpbuf[FM_RDS_BLK_SIZE];
1074 unsigned long flags;
1075 int ret;
1077 if (fmdev->rx.rds.wr_idx == fmdev->rx.rds.rd_idx) {
1078 if (file->f_flags & O_NONBLOCK)
1079 return -EWOULDBLOCK;
1081 ret = wait_event_interruptible(fmdev->rx.rds.read_queue,
1082 (fmdev->rx.rds.wr_idx != fmdev->rx.rds.rd_idx));
1083 if (ret)
1084 return -EINTR;
1087 /* Calculate block count from byte count */
1088 count /= FM_RDS_BLK_SIZE;
1089 block_count = 0;
1090 ret = 0;
1092 while (block_count < count) {
1093 spin_lock_irqsave(&fmdev->rds_buff_lock, flags);
1095 if (fmdev->rx.rds.wr_idx == fmdev->rx.rds.rd_idx) {
1096 spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
1097 break;
1099 memcpy(tmpbuf, &fmdev->rx.rds.buff[fmdev->rx.rds.rd_idx],
1100 FM_RDS_BLK_SIZE);
1101 fmdev->rx.rds.rd_idx += FM_RDS_BLK_SIZE;
1102 if (fmdev->rx.rds.rd_idx >= fmdev->rx.rds.buf_size)
1103 fmdev->rx.rds.rd_idx = 0;
1105 spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
1107 if (copy_to_user(buf, tmpbuf, FM_RDS_BLK_SIZE))
1108 break;
1110 block_count++;
1111 buf += FM_RDS_BLK_SIZE;
1112 ret += FM_RDS_BLK_SIZE;
1114 return ret;
1117 int fmc_set_freq(struct fmdev *fmdev, u32 freq_to_set)
1119 switch (fmdev->curr_fmmode) {
1120 case FM_MODE_RX:
1121 return fm_rx_set_freq(fmdev, freq_to_set);
1123 case FM_MODE_TX:
1124 return fm_tx_set_freq(fmdev, freq_to_set);
1126 default:
1127 return -EINVAL;
1131 int fmc_get_freq(struct fmdev *fmdev, u32 *cur_tuned_frq)
1133 if (fmdev->rx.freq == FM_UNDEFINED_FREQ) {
1134 fmerr("RX frequency is not set\n");
1135 return -EPERM;
1137 if (cur_tuned_frq == NULL) {
1138 fmerr("Invalid memory\n");
1139 return -ENOMEM;
1142 switch (fmdev->curr_fmmode) {
1143 case FM_MODE_RX:
1144 *cur_tuned_frq = fmdev->rx.freq;
1145 return 0;
1147 case FM_MODE_TX:
1148 *cur_tuned_frq = 0; /* TODO : Change this later */
1149 return 0;
1151 default:
1152 return -EINVAL;
1157 int fmc_set_region(struct fmdev *fmdev, u8 region_to_set)
1159 switch (fmdev->curr_fmmode) {
1160 case FM_MODE_RX:
1161 return fm_rx_set_region(fmdev, region_to_set);
1163 case FM_MODE_TX:
1164 return fm_tx_set_region(fmdev, region_to_set);
1166 default:
1167 return -EINVAL;
1171 int fmc_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
1173 switch (fmdev->curr_fmmode) {
1174 case FM_MODE_RX:
1175 return fm_rx_set_mute_mode(fmdev, mute_mode_toset);
1177 case FM_MODE_TX:
1178 return fm_tx_set_mute_mode(fmdev, mute_mode_toset);
1180 default:
1181 return -EINVAL;
1185 int fmc_set_stereo_mono(struct fmdev *fmdev, u16 mode)
1187 switch (fmdev->curr_fmmode) {
1188 case FM_MODE_RX:
1189 return fm_rx_set_stereo_mono(fmdev, mode);
1191 case FM_MODE_TX:
1192 return fm_tx_set_stereo_mono(fmdev, mode);
1194 default:
1195 return -EINVAL;
1199 int fmc_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
1201 switch (fmdev->curr_fmmode) {
1202 case FM_MODE_RX:
1203 return fm_rx_set_rds_mode(fmdev, rds_en_dis);
1205 case FM_MODE_TX:
1206 return fm_tx_set_rds_mode(fmdev, rds_en_dis);
1208 default:
1209 return -EINVAL;
1213 /* Sends power off command to the chip */
1214 static int fm_power_down(struct fmdev *fmdev)
1216 u16 payload;
1217 int ret;
1219 if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
1220 fmerr("FM core is not ready\n");
1221 return -EPERM;
1223 if (fmdev->curr_fmmode == FM_MODE_OFF) {
1224 fmdbg("FM chip is already in OFF state\n");
1225 return 0;
1228 payload = 0x0;
1229 ret = fmc_send_cmd(fmdev, FM_POWER_MODE, REG_WR, &payload,
1230 sizeof(payload), NULL, NULL);
1231 if (ret < 0)
1232 return ret;
1234 return fmc_release(fmdev);
1237 /* Reads init command from FM firmware file and loads to the chip */
1238 static int fm_download_firmware(struct fmdev *fmdev, const u8 *fw_name)
1240 const struct firmware *fw_entry;
1241 struct bts_header *fw_header;
1242 struct bts_action *action;
1243 struct bts_action_delay *delay;
1244 u8 *fw_data;
1245 int ret, fw_len, cmd_cnt;
1247 cmd_cnt = 0;
1248 set_bit(FM_FW_DW_INPROGRESS, &fmdev->flag);
1250 ret = request_firmware(&fw_entry, fw_name,
1251 &fmdev->radio_dev->dev);
1252 if (ret < 0) {
1253 fmerr("Unable to read firmware(%s) content\n", fw_name);
1254 return ret;
1256 fmdbg("Firmware(%s) length : %zu bytes\n", fw_name, fw_entry->size);
1258 fw_data = (void *)fw_entry->data;
1259 fw_len = fw_entry->size;
1261 fw_header = (struct bts_header *)fw_data;
1262 if (fw_header->magic != FM_FW_FILE_HEADER_MAGIC) {
1263 fmerr("%s not a legal TI firmware file\n", fw_name);
1264 ret = -EINVAL;
1265 goto rel_fw;
1267 fmdbg("FW(%s) magic number : 0x%x\n", fw_name, fw_header->magic);
1269 /* Skip file header info , we already verified it */
1270 fw_data += sizeof(struct bts_header);
1271 fw_len -= sizeof(struct bts_header);
1273 while (fw_data && fw_len > 0) {
1274 action = (struct bts_action *)fw_data;
1276 switch (action->type) {
1277 case ACTION_SEND_COMMAND: /* Send */
1278 if (fmc_send_cmd(fmdev, 0, 0, action->data,
1279 action->size, NULL, NULL))
1280 goto rel_fw;
1282 cmd_cnt++;
1283 break;
1285 case ACTION_DELAY: /* Delay */
1286 delay = (struct bts_action_delay *)action->data;
1287 mdelay(delay->msec);
1288 break;
1291 fw_data += (sizeof(struct bts_action) + (action->size));
1292 fw_len -= (sizeof(struct bts_action) + (action->size));
1294 fmdbg("Firmware commands(%d) loaded to chip\n", cmd_cnt);
1295 rel_fw:
1296 release_firmware(fw_entry);
1297 clear_bit(FM_FW_DW_INPROGRESS, &fmdev->flag);
1299 return ret;
1302 /* Loads default RX configuration to the chip */
1303 static int load_default_rx_configuration(struct fmdev *fmdev)
1305 int ret;
1307 ret = fm_rx_set_volume(fmdev, FM_DEFAULT_RX_VOLUME);
1308 if (ret < 0)
1309 return ret;
1311 return fm_rx_set_rssi_threshold(fmdev, FM_DEFAULT_RSSI_THRESHOLD);
1314 /* Does FM power on sequence */
1315 static int fm_power_up(struct fmdev *fmdev, u8 mode)
1317 u16 payload;
1318 __be16 asic_id, asic_ver;
1319 int resp_len, ret;
1320 u8 fw_name[50];
1322 if (mode >= FM_MODE_ENTRY_MAX) {
1323 fmerr("Invalid firmware download option\n");
1324 return -EINVAL;
1328 * Initialize FM common module. FM GPIO toggling is
1329 * taken care in Shared Transport driver.
1331 ret = fmc_prepare(fmdev);
1332 if (ret < 0) {
1333 fmerr("Unable to prepare FM Common\n");
1334 return ret;
1337 payload = FM_ENABLE;
1338 if (fmc_send_cmd(fmdev, FM_POWER_MODE, REG_WR, &payload,
1339 sizeof(payload), NULL, NULL))
1340 goto rel;
1342 /* Allow the chip to settle down in Channel-8 mode */
1343 msleep(20);
1345 if (fmc_send_cmd(fmdev, ASIC_ID_GET, REG_RD, NULL,
1346 sizeof(asic_id), &asic_id, &resp_len))
1347 goto rel;
1349 if (fmc_send_cmd(fmdev, ASIC_VER_GET, REG_RD, NULL,
1350 sizeof(asic_ver), &asic_ver, &resp_len))
1351 goto rel;
1353 fmdbg("ASIC ID: 0x%x , ASIC Version: %d\n",
1354 be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
1356 sprintf(fw_name, "%s_%x.%d.bts", FM_FMC_FW_FILE_START,
1357 be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
1359 ret = fm_download_firmware(fmdev, fw_name);
1360 if (ret < 0) {
1361 fmdbg("Failed to download firmware file %s\n", fw_name);
1362 goto rel;
1364 sprintf(fw_name, "%s_%x.%d.bts", (mode == FM_MODE_RX) ?
1365 FM_RX_FW_FILE_START : FM_TX_FW_FILE_START,
1366 be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
1368 ret = fm_download_firmware(fmdev, fw_name);
1369 if (ret < 0) {
1370 fmdbg("Failed to download firmware file %s\n", fw_name);
1371 goto rel;
1372 } else
1373 return ret;
1374 rel:
1375 return fmc_release(fmdev);
1378 /* Set FM Modes(TX, RX, OFF) */
1379 int fmc_set_mode(struct fmdev *fmdev, u8 fm_mode)
1381 int ret = 0;
1383 if (fm_mode >= FM_MODE_ENTRY_MAX) {
1384 fmerr("Invalid FM mode\n");
1385 return -EINVAL;
1387 if (fmdev->curr_fmmode == fm_mode) {
1388 fmdbg("Already fm is in mode(%d)\n", fm_mode);
1389 return ret;
1392 switch (fm_mode) {
1393 case FM_MODE_OFF: /* OFF Mode */
1394 ret = fm_power_down(fmdev);
1395 if (ret < 0) {
1396 fmerr("Failed to set OFF mode\n");
1397 return ret;
1399 break;
1401 case FM_MODE_TX: /* TX Mode */
1402 case FM_MODE_RX: /* RX Mode */
1403 /* Power down before switching to TX or RX mode */
1404 if (fmdev->curr_fmmode != FM_MODE_OFF) {
1405 ret = fm_power_down(fmdev);
1406 if (ret < 0) {
1407 fmerr("Failed to set OFF mode\n");
1408 return ret;
1410 msleep(30);
1412 ret = fm_power_up(fmdev, fm_mode);
1413 if (ret < 0) {
1414 fmerr("Failed to load firmware\n");
1415 return ret;
1418 fmdev->curr_fmmode = fm_mode;
1420 /* Set default configuration */
1421 if (fmdev->curr_fmmode == FM_MODE_RX) {
1422 fmdbg("Loading default rx configuration..\n");
1423 ret = load_default_rx_configuration(fmdev);
1424 if (ret < 0)
1425 fmerr("Failed to load default values\n");
1428 return ret;
1431 /* Returns current FM mode (TX, RX, OFF) */
1432 int fmc_get_mode(struct fmdev *fmdev, u8 *fmmode)
1434 if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
1435 fmerr("FM core is not ready\n");
1436 return -EPERM;
1438 if (fmmode == NULL) {
1439 fmerr("Invalid memory\n");
1440 return -ENOMEM;
1443 *fmmode = fmdev->curr_fmmode;
1444 return 0;
1447 /* Called by ST layer when FM packet is available */
1448 static long fm_st_receive(void *arg, struct sk_buff *skb)
1450 struct fmdev *fmdev;
1452 fmdev = (struct fmdev *)arg;
1454 if (skb == NULL) {
1455 fmerr("Invalid SKB received from ST\n");
1456 return -EFAULT;
1459 if (skb->cb[0] != FM_PKT_LOGICAL_CHAN_NUMBER) {
1460 fmerr("Received SKB (%p) is not FM Channel 8 pkt\n", skb);
1461 return -EINVAL;
1464 memcpy(skb_push(skb, 1), &skb->cb[0], 1);
1465 skb_queue_tail(&fmdev->rx_q, skb);
1466 tasklet_schedule(&fmdev->rx_task);
1468 return 0;
1472 * Called by ST layer to indicate protocol registration completion
1473 * status.
1475 static void fm_st_reg_comp_cb(void *arg, char data)
1477 struct fmdev *fmdev;
1479 fmdev = (struct fmdev *)arg;
1480 fmdev->streg_cbdata = data;
1481 complete(&wait_for_fmdrv_reg_comp);
1485 * This function will be called from FM V4L2 open function.
1486 * Register with ST driver and initialize driver data.
1488 int fmc_prepare(struct fmdev *fmdev)
1490 static struct st_proto_s fm_st_proto;
1491 int ret;
1493 if (test_bit(FM_CORE_READY, &fmdev->flag)) {
1494 fmdbg("FM Core is already up\n");
1495 return 0;
1498 memset(&fm_st_proto, 0, sizeof(fm_st_proto));
1499 fm_st_proto.recv = fm_st_receive;
1500 fm_st_proto.match_packet = NULL;
1501 fm_st_proto.reg_complete_cb = fm_st_reg_comp_cb;
1502 fm_st_proto.write = NULL; /* TI ST driver will fill write pointer */
1503 fm_st_proto.priv_data = fmdev;
1504 fm_st_proto.chnl_id = 0x08;
1505 fm_st_proto.max_frame_size = 0xff;
1506 fm_st_proto.hdr_len = 1;
1507 fm_st_proto.offset_len_in_hdr = 0;
1508 fm_st_proto.len_size = 1;
1509 fm_st_proto.reserve = 1;
1511 ret = st_register(&fm_st_proto);
1512 if (ret == -EINPROGRESS) {
1513 init_completion(&wait_for_fmdrv_reg_comp);
1514 fmdev->streg_cbdata = -EINPROGRESS;
1515 fmdbg("%s waiting for ST reg completion signal\n", __func__);
1517 if (!wait_for_completion_timeout(&wait_for_fmdrv_reg_comp,
1518 FM_ST_REG_TIMEOUT)) {
1519 fmerr("Timeout(%d sec), didn't get reg "
1520 "completion signal from ST\n",
1521 jiffies_to_msecs(FM_ST_REG_TIMEOUT) / 1000);
1522 return -ETIMEDOUT;
1524 if (fmdev->streg_cbdata != 0) {
1525 fmerr("ST reg comp CB called with error "
1526 "status %d\n", fmdev->streg_cbdata);
1527 return -EAGAIN;
1530 ret = 0;
1531 } else if (ret == -1) {
1532 fmerr("st_register failed %d\n", ret);
1533 return -EAGAIN;
1536 if (fm_st_proto.write != NULL) {
1537 g_st_write = fm_st_proto.write;
1538 } else {
1539 fmerr("Failed to get ST write func pointer\n");
1540 ret = st_unregister(&fm_st_proto);
1541 if (ret < 0)
1542 fmerr("st_unregister failed %d\n", ret);
1543 return -EAGAIN;
1546 spin_lock_init(&fmdev->rds_buff_lock);
1547 spin_lock_init(&fmdev->resp_skb_lock);
1549 /* Initialize TX queue and TX tasklet */
1550 skb_queue_head_init(&fmdev->tx_q);
1551 tasklet_init(&fmdev->tx_task, send_tasklet, (unsigned long)fmdev);
1553 /* Initialize RX Queue and RX tasklet */
1554 skb_queue_head_init(&fmdev->rx_q);
1555 tasklet_init(&fmdev->rx_task, recv_tasklet, (unsigned long)fmdev);
1557 fmdev->irq_info.stage = 0;
1558 atomic_set(&fmdev->tx_cnt, 1);
1559 fmdev->resp_comp = NULL;
1561 init_timer(&fmdev->irq_info.timer);
1562 fmdev->irq_info.timer.function = &int_timeout_handler;
1563 fmdev->irq_info.timer.data = (unsigned long)fmdev;
1564 /*TODO: add FM_STIC_EVENT later */
1565 fmdev->irq_info.mask = FM_MAL_EVENT;
1567 /* Region info */
1568 fmdev->rx.region = region_configs[default_radio_region];
1570 fmdev->rx.mute_mode = FM_MUTE_OFF;
1571 fmdev->rx.rf_depend_mute = FM_RX_RF_DEPENDENT_MUTE_OFF;
1572 fmdev->rx.rds.flag = FM_RDS_DISABLE;
1573 fmdev->rx.freq = FM_UNDEFINED_FREQ;
1574 fmdev->rx.rds_mode = FM_RDS_SYSTEM_RDS;
1575 fmdev->rx.af_mode = FM_RX_RDS_AF_SWITCH_MODE_OFF;
1576 fmdev->irq_info.retry = 0;
1578 fm_rx_reset_rds_cache(fmdev);
1579 init_waitqueue_head(&fmdev->rx.rds.read_queue);
1581 fm_rx_reset_station_info(fmdev);
1582 set_bit(FM_CORE_READY, &fmdev->flag);
1584 return ret;
1588 * This function will be called from FM V4L2 release function.
1589 * Unregister from ST driver.
1591 int fmc_release(struct fmdev *fmdev)
1593 static struct st_proto_s fm_st_proto;
1594 int ret;
1596 if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
1597 fmdbg("FM Core is already down\n");
1598 return 0;
1600 /* Service pending read */
1601 wake_up_interruptible(&fmdev->rx.rds.read_queue);
1603 tasklet_kill(&fmdev->tx_task);
1604 tasklet_kill(&fmdev->rx_task);
1606 skb_queue_purge(&fmdev->tx_q);
1607 skb_queue_purge(&fmdev->rx_q);
1609 fmdev->resp_comp = NULL;
1610 fmdev->rx.freq = 0;
1612 memset(&fm_st_proto, 0, sizeof(fm_st_proto));
1613 fm_st_proto.chnl_id = 0x08;
1615 ret = st_unregister(&fm_st_proto);
1617 if (ret < 0)
1618 fmerr("Failed to de-register FM from ST %d\n", ret);
1619 else
1620 fmdbg("Successfully unregistered from ST\n");
1622 clear_bit(FM_CORE_READY, &fmdev->flag);
1623 return ret;
1627 * Module init function. Ask FM V4L module to register video device.
1628 * Allocate memory for FM driver context and RX RDS buffer.
1630 static int __init fm_drv_init(void)
1632 struct fmdev *fmdev = NULL;
1633 int ret = -ENOMEM;
1635 fmdbg("FM driver version %s\n", FM_DRV_VERSION);
1637 fmdev = kzalloc(sizeof(struct fmdev), GFP_KERNEL);
1638 if (NULL == fmdev) {
1639 fmerr("Can't allocate operation structure memory\n");
1640 return ret;
1642 fmdev->rx.rds.buf_size = default_rds_buf * FM_RDS_BLK_SIZE;
1643 fmdev->rx.rds.buff = kzalloc(fmdev->rx.rds.buf_size, GFP_KERNEL);
1644 if (NULL == fmdev->rx.rds.buff) {
1645 fmerr("Can't allocate rds ring buffer\n");
1646 goto rel_dev;
1649 ret = fm_v4l2_init_video_device(fmdev, radio_nr);
1650 if (ret < 0)
1651 goto rel_rdsbuf;
1653 fmdev->irq_info.handlers = int_handler_table;
1654 fmdev->curr_fmmode = FM_MODE_OFF;
1655 fmdev->tx_data.pwr_lvl = FM_PWR_LVL_DEF;
1656 fmdev->tx_data.preemph = FM_TX_PREEMPH_50US;
1657 return ret;
1659 rel_rdsbuf:
1660 kfree(fmdev->rx.rds.buff);
1661 rel_dev:
1662 kfree(fmdev);
1664 return ret;
1667 /* Module exit function. Ask FM V4L module to unregister video device */
1668 static void __exit fm_drv_exit(void)
1670 struct fmdev *fmdev = NULL;
1672 fmdev = fm_v4l2_deinit_video_device();
1673 if (fmdev != NULL) {
1674 kfree(fmdev->rx.rds.buff);
1675 kfree(fmdev);
1679 module_init(fm_drv_init);
1680 module_exit(fm_drv_exit);
1682 /* ------------- Module Info ------------- */
1683 MODULE_AUTHOR("Manjunatha Halli <manjunatha_halli@ti.com>");
1684 MODULE_DESCRIPTION("FM Driver for TI's Connectivity chip. " FM_DRV_VERSION);
1685 MODULE_VERSION(FM_DRV_VERSION);
1686 MODULE_LICENSE("GPL");