1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  FM Driver for Connectivity chip of Texas Instruments.
4  *
5  *  This sub-module of FM driver is common for FM RX and TX
6  *  functionality. This module is responsible for:
7  *  1) Forming group of Channel-8 commands to perform particular
8  *     functionality (eg., frequency set require more than
9  *     one Channel-8 command to be sent to the chip).
10  *  2) Sending each Channel-8 command to the chip and reading
11  *     response back over Shared Transport.
12  *  3) Managing TX and RX Queues and Tasklets.
13  *  4) Handling FM Interrupt packet and taking appropriate action.
14  *  5) Loading FM firmware to the chip (common, FM TX, and FM RX
15  *     firmware files based on mode selection)
16  *
17  *  Copyright (C) 2011 Texas Instruments
18  *  Author: Raja Mani <raja_mani@ti.com>
19  *  Author: Manjunatha Halli <manjunatha_halli@ti.com>
20  */
21 
22 #include <linux/module.h>
23 #include <linux/firmware.h>
24 #include <linux/delay.h>
25 #include "fmdrv.h"
26 #include "fmdrv_v4l2.h"
27 #include "fmdrv_common.h"
28 #include <linux/ti_wilink_st.h>
29 #include "fmdrv_rx.h"
30 #include "fmdrv_tx.h"
31 
32 /* Region info */
33 static struct region_info region_configs[] = {
34 	/* Europe/US */
35 	{
36 	 .chanl_space = FM_CHANNEL_SPACING_200KHZ * FM_FREQ_MUL,
37 	 .bot_freq = 87500,	/* 87.5 MHz */
38 	 .top_freq = 108000,	/* 108 MHz */
39 	 .fm_band = 0,
40 	 },
41 	/* Japan */
42 	{
43 	 .chanl_space = FM_CHANNEL_SPACING_200KHZ * FM_FREQ_MUL,
44 	 .bot_freq = 76000,	/* 76 MHz */
45 	 .top_freq = 90000,	/* 90 MHz */
46 	 .fm_band = 1,
47 	 },
48 };
49 
50 /* Band selection */
51 static u8 default_radio_region;	/* Europe/US */
52 module_param(default_radio_region, byte, 0);
53 MODULE_PARM_DESC(default_radio_region, "Region: 0=Europe/US, 1=Japan");
54 
55 /* RDS buffer blocks */
56 static u32 default_rds_buf = 300;
57 module_param(default_rds_buf, uint, 0444);
58 MODULE_PARM_DESC(default_rds_buf, "RDS buffer entries");
59 
60 /* Radio Nr */
61 static u32 radio_nr = -1;
62 module_param(radio_nr, int, 0444);
63 MODULE_PARM_DESC(radio_nr, "Radio Nr");
64 
65 /* FM irq handlers forward declaration */
66 static void fm_irq_send_flag_getcmd(struct fmdev *);
67 static void fm_irq_handle_flag_getcmd_resp(struct fmdev *);
68 static void fm_irq_handle_hw_malfunction(struct fmdev *);
69 static void fm_irq_handle_rds_start(struct fmdev *);
70 static void fm_irq_send_rdsdata_getcmd(struct fmdev *);
71 static void fm_irq_handle_rdsdata_getcmd_resp(struct fmdev *);
72 static void fm_irq_handle_rds_finish(struct fmdev *);
73 static void fm_irq_handle_tune_op_ended(struct fmdev *);
74 static void fm_irq_handle_power_enb(struct fmdev *);
75 static void fm_irq_handle_low_rssi_start(struct fmdev *);
76 static void fm_irq_afjump_set_pi(struct fmdev *);
77 static void fm_irq_handle_set_pi_resp(struct fmdev *);
78 static void fm_irq_afjump_set_pimask(struct fmdev *);
79 static void fm_irq_handle_set_pimask_resp(struct fmdev *);
80 static void fm_irq_afjump_setfreq(struct fmdev *);
81 static void fm_irq_handle_setfreq_resp(struct fmdev *);
82 static void fm_irq_afjump_enableint(struct fmdev *);
83 static void fm_irq_afjump_enableint_resp(struct fmdev *);
84 static void fm_irq_start_afjump(struct fmdev *);
85 static void fm_irq_handle_start_afjump_resp(struct fmdev *);
86 static void fm_irq_afjump_rd_freq(struct fmdev *);
87 static void fm_irq_afjump_rd_freq_resp(struct fmdev *);
88 static void fm_irq_handle_low_rssi_finish(struct fmdev *);
89 static void fm_irq_send_intmsk_cmd(struct fmdev *);
90 static void fm_irq_handle_intmsk_cmd_resp(struct fmdev *);
91 
92 /*
93  * When FM common module receives interrupt packet, following handlers
94  * will be executed one after another to service the interrupt(s)
95  */
96 enum fmc_irq_handler_index {
97 	FM_SEND_FLAG_GETCMD_IDX,
98 	FM_HANDLE_FLAG_GETCMD_RESP_IDX,
99 
100 	/* HW malfunction irq handler */
101 	FM_HW_MAL_FUNC_IDX,
102 
103 	/* RDS threshold reached irq handler */
104 	FM_RDS_START_IDX,
105 	FM_RDS_SEND_RDS_GETCMD_IDX,
106 	FM_RDS_HANDLE_RDS_GETCMD_RESP_IDX,
107 	FM_RDS_FINISH_IDX,
108 
109 	/* Tune operation ended irq handler */
110 	FM_HW_TUNE_OP_ENDED_IDX,
111 
112 	/* TX power enable irq handler */
113 	FM_HW_POWER_ENB_IDX,
114 
115 	/* Low RSSI irq handler */
116 	FM_LOW_RSSI_START_IDX,
117 	FM_AF_JUMP_SETPI_IDX,
118 	FM_AF_JUMP_HANDLE_SETPI_RESP_IDX,
119 	FM_AF_JUMP_SETPI_MASK_IDX,
120 	FM_AF_JUMP_HANDLE_SETPI_MASK_RESP_IDX,
121 	FM_AF_JUMP_SET_AF_FREQ_IDX,
122 	FM_AF_JUMP_HANDLE_SET_AFFREQ_RESP_IDX,
123 	FM_AF_JUMP_ENABLE_INT_IDX,
124 	FM_AF_JUMP_ENABLE_INT_RESP_IDX,
125 	FM_AF_JUMP_START_AFJUMP_IDX,
126 	FM_AF_JUMP_HANDLE_START_AFJUMP_RESP_IDX,
127 	FM_AF_JUMP_RD_FREQ_IDX,
128 	FM_AF_JUMP_RD_FREQ_RESP_IDX,
129 	FM_LOW_RSSI_FINISH_IDX,
130 
131 	/* Interrupt process post action */
132 	FM_SEND_INTMSK_CMD_IDX,
133 	FM_HANDLE_INTMSK_CMD_RESP_IDX,
134 };
135 
136 /* FM interrupt handler table */
137 static int_handler_prototype int_handler_table[] = {
138 	fm_irq_send_flag_getcmd,
139 	fm_irq_handle_flag_getcmd_resp,
140 	fm_irq_handle_hw_malfunction,
141 	fm_irq_handle_rds_start, /* RDS threshold reached irq handler */
142 	fm_irq_send_rdsdata_getcmd,
143 	fm_irq_handle_rdsdata_getcmd_resp,
144 	fm_irq_handle_rds_finish,
145 	fm_irq_handle_tune_op_ended,
146 	fm_irq_handle_power_enb, /* TX power enable irq handler */
147 	fm_irq_handle_low_rssi_start,
148 	fm_irq_afjump_set_pi,
149 	fm_irq_handle_set_pi_resp,
150 	fm_irq_afjump_set_pimask,
151 	fm_irq_handle_set_pimask_resp,
152 	fm_irq_afjump_setfreq,
153 	fm_irq_handle_setfreq_resp,
154 	fm_irq_afjump_enableint,
155 	fm_irq_afjump_enableint_resp,
156 	fm_irq_start_afjump,
157 	fm_irq_handle_start_afjump_resp,
158 	fm_irq_afjump_rd_freq,
159 	fm_irq_afjump_rd_freq_resp,
160 	fm_irq_handle_low_rssi_finish,
161 	fm_irq_send_intmsk_cmd, /* Interrupt process post action */
162 	fm_irq_handle_intmsk_cmd_resp
163 };
164 
165 static long (*g_st_write) (struct sk_buff *skb);
166 static struct completion wait_for_fmdrv_reg_comp;
167 
168 static inline void fm_irq_call(struct fmdev *fmdev)
169 {
170 	fmdev->irq_info.handlers[fmdev->irq_info.stage](fmdev);
171 }
172 
173 /* Continue next function in interrupt handler table */
174 static inline void fm_irq_call_stage(struct fmdev *fmdev, u8 stage)
175 {
176 	fmdev->irq_info.stage = stage;
177 	fm_irq_call(fmdev);
178 }
179 
180 static inline void fm_irq_timeout_stage(struct fmdev *fmdev, u8 stage)
181 {
182 	fmdev->irq_info.stage = stage;
183 	mod_timer(&fmdev->irq_info.timer, jiffies + FM_DRV_TX_TIMEOUT);
184 }
185 
186 #ifdef FM_DUMP_TXRX_PKT
187  /* To dump outgoing FM Channel-8 packets */
188 inline void dump_tx_skb_data(struct sk_buff *skb)
189 {
190 	int len, len_org;
191 	u8 index;
192 	struct fm_cmd_msg_hdr *cmd_hdr;
193 
194 	cmd_hdr = (struct fm_cmd_msg_hdr *)skb->data;
195 	printk(KERN_INFO "<<%shdr:%02x len:%02x opcode:%02x type:%s dlen:%02x",
196 	       fm_cb(skb)->completion ? " " : "*", cmd_hdr->hdr,
197 	       cmd_hdr->len, cmd_hdr->op,
198 	       cmd_hdr->rd_wr ? "RD" : "WR", cmd_hdr->dlen);
199 
200 	len_org = skb->len - FM_CMD_MSG_HDR_SIZE;
201 	if (len_org > 0) {
202 		printk(KERN_CONT "\n   data(%d): ", cmd_hdr->dlen);
203 		len = min(len_org, 14);
204 		for (index = 0; index < len; index++)
205 			printk(KERN_CONT "%x ",
206 			       skb->data[FM_CMD_MSG_HDR_SIZE + index]);
207 		printk(KERN_CONT "%s", (len_org > 14) ? ".." : "");
208 	}
209 	printk(KERN_CONT "\n");
210 }
211 
212  /* To dump incoming FM Channel-8 packets */
213 inline void dump_rx_skb_data(struct sk_buff *skb)
214 {
215 	int len, len_org;
216 	u8 index;
217 	struct fm_event_msg_hdr *evt_hdr;
218 
219 	evt_hdr = (struct fm_event_msg_hdr *)skb->data;
220 	printk(KERN_INFO ">> hdr:%02x len:%02x sts:%02x numhci:%02x opcode:%02x type:%s dlen:%02x",
221 	       evt_hdr->hdr, evt_hdr->len,
222 	       evt_hdr->status, evt_hdr->num_fm_hci_cmds, evt_hdr->op,
223 	       (evt_hdr->rd_wr) ? "RD" : "WR", evt_hdr->dlen);
224 
225 	len_org = skb->len - FM_EVT_MSG_HDR_SIZE;
226 	if (len_org > 0) {
227 		printk(KERN_CONT "\n   data(%d): ", evt_hdr->dlen);
228 		len = min(len_org, 14);
229 		for (index = 0; index < len; index++)
230 			printk(KERN_CONT "%x ",
231 			       skb->data[FM_EVT_MSG_HDR_SIZE + index]);
232 		printk(KERN_CONT "%s", (len_org > 14) ? ".." : "");
233 	}
234 	printk(KERN_CONT "\n");
235 }
236 #endif
237 
238 void fmc_update_region_info(struct fmdev *fmdev, u8 region_to_set)
239 {
240 	fmdev->rx.region = region_configs[region_to_set];
241 }
242 
243 /*
244  * FM common sub-module will schedule this tasklet whenever it receives
245  * FM packet from ST driver.
246  */
247 static void recv_tasklet(struct tasklet_struct *t)
248 {
249 	struct fmdev *fmdev;
250 	struct fm_irq *irq_info;
251 	struct fm_event_msg_hdr *evt_hdr;
252 	struct sk_buff *skb;
253 	u8 num_fm_hci_cmds;
254 	unsigned long flags;
255 
256 	fmdev = from_tasklet(fmdev, t, tx_task);
257 	irq_info = &fmdev->irq_info;
258 	/* Process all packets in the RX queue */
259 	while ((skb = skb_dequeue(&fmdev->rx_q))) {
260 		if (skb->len < sizeof(struct fm_event_msg_hdr)) {
261 			fmerr("skb(%p) has only %d bytes, at least need %zu bytes to decode\n",
262 			      skb,
263 			      skb->len, sizeof(struct fm_event_msg_hdr));
264 			kfree_skb(skb);
265 			continue;
266 		}
267 
268 		evt_hdr = (void *)skb->data;
269 		num_fm_hci_cmds = evt_hdr->num_fm_hci_cmds;
270 
271 		/* FM interrupt packet? */
272 		if (evt_hdr->op == FM_INTERRUPT) {
273 			/* FM interrupt handler started already? */
274 			if (!test_bit(FM_INTTASK_RUNNING, &fmdev->flag)) {
275 				set_bit(FM_INTTASK_RUNNING, &fmdev->flag);
276 				if (irq_info->stage != 0) {
277 					fmerr("Inval stage resetting to zero\n");
278 					irq_info->stage = 0;
279 				}
280 
281 				/*
282 				 * Execute first function in interrupt handler
283 				 * table.
284 				 */
285 				irq_info->handlers[irq_info->stage](fmdev);
286 			} else {
287 				set_bit(FM_INTTASK_SCHEDULE_PENDING, &fmdev->flag);
288 			}
289 			kfree_skb(skb);
290 		}
291 		/* Anyone waiting for this with completion handler? */
292 		else if (evt_hdr->op == fmdev->pre_op && fmdev->resp_comp != NULL) {
293 
294 			spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
295 			fmdev->resp_skb = skb;
296 			spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
297 			complete(fmdev->resp_comp);
298 
299 			fmdev->resp_comp = NULL;
300 			atomic_set(&fmdev->tx_cnt, 1);
301 		}
302 		/* Is this for interrupt handler? */
303 		else if (evt_hdr->op == fmdev->pre_op && fmdev->resp_comp == NULL) {
304 			if (fmdev->resp_skb != NULL)
305 				fmerr("Response SKB ptr not NULL\n");
306 
307 			spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
308 			fmdev->resp_skb = skb;
309 			spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
310 
311 			/* Execute interrupt handler where state index points */
312 			irq_info->handlers[irq_info->stage](fmdev);
313 
314 			kfree_skb(skb);
315 			atomic_set(&fmdev->tx_cnt, 1);
316 		} else {
317 			fmerr("Nobody claimed SKB(%p),purging\n", skb);
318 		}
319 
320 		/*
321 		 * Check flow control field. If Num_FM_HCI_Commands field is
322 		 * not zero, schedule FM TX tasklet.
323 		 */
324 		if (num_fm_hci_cmds && atomic_read(&fmdev->tx_cnt))
325 			if (!skb_queue_empty(&fmdev->tx_q))
326 				tasklet_schedule(&fmdev->tx_task);
327 	}
328 }
329 
330 /* FM send tasklet: is scheduled when FM packet has to be sent to chip */
331 static void send_tasklet(struct tasklet_struct *t)
332 {
333 	struct fmdev *fmdev;
334 	struct sk_buff *skb;
335 	int len;
336 
337 	fmdev = from_tasklet(fmdev, t, tx_task);
338 
339 	if (!atomic_read(&fmdev->tx_cnt))
340 		return;
341 
342 	/* Check, is there any timeout happened to last transmitted packet */
343 	if ((jiffies - fmdev->last_tx_jiffies) > FM_DRV_TX_TIMEOUT) {
344 		fmerr("TX timeout occurred\n");
345 		atomic_set(&fmdev->tx_cnt, 1);
346 	}
347 
348 	/* Send queued FM TX packets */
349 	skb = skb_dequeue(&fmdev->tx_q);
350 	if (!skb)
351 		return;
352 
353 	atomic_dec(&fmdev->tx_cnt);
354 	fmdev->pre_op = fm_cb(skb)->fm_op;
355 
356 	if (fmdev->resp_comp != NULL)
357 		fmerr("Response completion handler is not NULL\n");
358 
359 	fmdev->resp_comp = fm_cb(skb)->completion;
360 
361 	/* Write FM packet to ST driver */
362 	len = g_st_write(skb);
363 	if (len < 0) {
364 		kfree_skb(skb);
365 		fmdev->resp_comp = NULL;
366 		fmerr("TX tasklet failed to send skb(%p)\n", skb);
367 		atomic_set(&fmdev->tx_cnt, 1);
368 	} else {
369 		fmdev->last_tx_jiffies = jiffies;
370 	}
371 }
372 
373 /*
374  * Queues FM Channel-8 packet to FM TX queue and schedules FM TX tasklet for
375  * transmission
376  */
377 static int fm_send_cmd(struct fmdev *fmdev, u8 fm_op, u16 type,	void *payload,
378 		int payload_len, struct completion *wait_completion)
379 {
380 	struct sk_buff *skb;
381 	struct fm_cmd_msg_hdr *hdr;
382 	int size;
383 
384 	if (fm_op >= FM_INTERRUPT) {
385 		fmerr("Invalid fm opcode - %d\n", fm_op);
386 		return -EINVAL;
387 	}
388 	if (test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag) && payload == NULL) {
389 		fmerr("Payload data is NULL during fw download\n");
390 		return -EINVAL;
391 	}
392 	if (!test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag))
393 		size =
394 		    FM_CMD_MSG_HDR_SIZE + ((payload == NULL) ? 0 : payload_len);
395 	else
396 		size = payload_len;
397 
398 	skb = alloc_skb(size, GFP_ATOMIC);
399 	if (!skb) {
400 		fmerr("No memory to create new SKB\n");
401 		return -ENOMEM;
402 	}
403 	/*
404 	 * Don't fill FM header info for the commands which come from
405 	 * FM firmware file.
406 	 */
407 	if (!test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag) ||
408 			test_bit(FM_INTTASK_RUNNING, &fmdev->flag)) {
409 		/* Fill command header info */
410 		hdr = skb_put(skb, FM_CMD_MSG_HDR_SIZE);
411 		hdr->hdr = FM_PKT_LOGICAL_CHAN_NUMBER;	/* 0x08 */
412 
413 		/* 3 (fm_opcode,rd_wr,dlen) + payload len) */
414 		hdr->len = ((payload == NULL) ? 0 : payload_len) + 3;
415 
416 		/* FM opcode */
417 		hdr->op = fm_op;
418 
419 		/* read/write type */
420 		hdr->rd_wr = type;
421 		hdr->dlen = payload_len;
422 		fm_cb(skb)->fm_op = fm_op;
423 
424 		/*
425 		 * If firmware download has finished and the command is
426 		 * not a read command then payload is != NULL - a write
427 		 * command with u16 payload - convert to be16
428 		 */
429 		if (payload != NULL)
430 			*(__be16 *)payload = cpu_to_be16(*(u16 *)payload);
431 
432 	} else if (payload != NULL) {
433 		fm_cb(skb)->fm_op = *((u8 *)payload + 2);
434 	}
435 	if (payload != NULL)
436 		skb_put_data(skb, payload, payload_len);
437 
438 	fm_cb(skb)->completion = wait_completion;
439 	skb_queue_tail(&fmdev->tx_q, skb);
440 	tasklet_schedule(&fmdev->tx_task);
441 
442 	return 0;
443 }
444 
445 /* Sends FM Channel-8 command to the chip and waits for the response */
446 int fmc_send_cmd(struct fmdev *fmdev, u8 fm_op, u16 type, void *payload,
447 		unsigned int payload_len, void *response, int *response_len)
448 {
449 	struct sk_buff *skb;
450 	struct fm_event_msg_hdr *evt_hdr;
451 	unsigned long flags;
452 	int ret;
453 
454 	init_completion(&fmdev->maintask_comp);
455 	ret = fm_send_cmd(fmdev, fm_op, type, payload, payload_len,
456 			    &fmdev->maintask_comp);
457 	if (ret)
458 		return ret;
459 
460 	if (!wait_for_completion_timeout(&fmdev->maintask_comp,
461 					 FM_DRV_TX_TIMEOUT)) {
462 		fmerr("Timeout(%d sec),didn't get regcompletion signal from RX tasklet\n",
463 			   jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
464 		return -ETIMEDOUT;
465 	}
466 	if (!fmdev->resp_skb) {
467 		fmerr("Response SKB is missing\n");
468 		return -EFAULT;
469 	}
470 	spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
471 	skb = fmdev->resp_skb;
472 	fmdev->resp_skb = NULL;
473 	spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
474 
475 	evt_hdr = (void *)skb->data;
476 	if (evt_hdr->status != 0) {
477 		fmerr("Received event pkt status(%d) is not zero\n",
478 			   evt_hdr->status);
479 		kfree_skb(skb);
480 		return -EIO;
481 	}
482 	/* Send response data to caller */
483 	if (response != NULL && response_len != NULL && evt_hdr->dlen &&
484 	    evt_hdr->dlen <= payload_len) {
485 		/* Skip header info and copy only response data */
486 		skb_pull(skb, sizeof(struct fm_event_msg_hdr));
487 		memcpy(response, skb->data, evt_hdr->dlen);
488 		*response_len = evt_hdr->dlen;
489 	} else if (response_len != NULL && evt_hdr->dlen == 0) {
490 		*response_len = 0;
491 	}
492 	kfree_skb(skb);
493 
494 	return 0;
495 }
496 
497 /* --- Helper functions used in FM interrupt handlers ---*/
498 static inline int check_cmdresp_status(struct fmdev *fmdev,
499 		struct sk_buff **skb)
500 {
501 	struct fm_event_msg_hdr *fm_evt_hdr;
502 	unsigned long flags;
503 
504 	del_timer(&fmdev->irq_info.timer);
505 
506 	spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
507 	*skb = fmdev->resp_skb;
508 	fmdev->resp_skb = NULL;
509 	spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
510 
511 	fm_evt_hdr = (void *)(*skb)->data;
512 	if (fm_evt_hdr->status != 0) {
513 		fmerr("irq: opcode %x response status is not zero Initiating irq recovery process\n",
514 				fm_evt_hdr->op);
515 
516 		mod_timer(&fmdev->irq_info.timer, jiffies + FM_DRV_TX_TIMEOUT);
517 		return -1;
518 	}
519 
520 	return 0;
521 }
522 
523 static inline void fm_irq_common_cmd_resp_helper(struct fmdev *fmdev, u8 stage)
524 {
525 	struct sk_buff *skb;
526 
527 	if (!check_cmdresp_status(fmdev, &skb))
528 		fm_irq_call_stage(fmdev, stage);
529 }
530 
531 /*
532  * Interrupt process timeout handler.
533  * One of the irq handler did not get proper response from the chip. So take
534  * recovery action here. FM interrupts are disabled in the beginning of
535  * interrupt process. Therefore reset stage index to re-enable default
536  * interrupts. So that next interrupt will be processed as usual.
537  */
538 static void int_timeout_handler(struct timer_list *t)
539 {
540 	struct fmdev *fmdev;
541 	struct fm_irq *fmirq;
542 
543 	fmdbg("irq: timeout,trying to re-enable fm interrupts\n");
544 	fmdev = from_timer(fmdev, t, irq_info.timer);
545 	fmirq = &fmdev->irq_info;
546 	fmirq->retry++;
547 
548 	if (fmirq->retry > FM_IRQ_TIMEOUT_RETRY_MAX) {
549 		/* Stop recovery action (interrupt reenable process) and
550 		 * reset stage index & retry count values */
551 		fmirq->stage = 0;
552 		fmirq->retry = 0;
553 		fmerr("Recovery action failed duringirq processing, max retry reached\n");
554 		return;
555 	}
556 	fm_irq_call_stage(fmdev, FM_SEND_INTMSK_CMD_IDX);
557 }
558 
559 /* --------- FM interrupt handlers ------------*/
560 static void fm_irq_send_flag_getcmd(struct fmdev *fmdev)
561 {
562 	u16 flag;
563 
564 	/* Send FLAG_GET command , to know the source of interrupt */
565 	if (!fm_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, sizeof(flag), NULL))
566 		fm_irq_timeout_stage(fmdev, FM_HANDLE_FLAG_GETCMD_RESP_IDX);
567 }
568 
569 static void fm_irq_handle_flag_getcmd_resp(struct fmdev *fmdev)
570 {
571 	struct sk_buff *skb;
572 	struct fm_event_msg_hdr *fm_evt_hdr;
573 
574 	if (check_cmdresp_status(fmdev, &skb))
575 		return;
576 
577 	fm_evt_hdr = (void *)skb->data;
578 	if (fm_evt_hdr->dlen > sizeof(fmdev->irq_info.flag))
579 		return;
580 
581 	/* Skip header info and copy only response data */
582 	skb_pull(skb, sizeof(struct fm_event_msg_hdr));
583 	memcpy(&fmdev->irq_info.flag, skb->data, fm_evt_hdr->dlen);
584 
585 	fmdev->irq_info.flag = be16_to_cpu((__force __be16)fmdev->irq_info.flag);
586 	fmdbg("irq: flag register(0x%x)\n", fmdev->irq_info.flag);
587 
588 	/* Continue next function in interrupt handler table */
589 	fm_irq_call_stage(fmdev, FM_HW_MAL_FUNC_IDX);
590 }
591 
592 static void fm_irq_handle_hw_malfunction(struct fmdev *fmdev)
593 {
594 	if (fmdev->irq_info.flag & FM_MAL_EVENT & fmdev->irq_info.mask)
595 		fmerr("irq: HW MAL int received - do nothing\n");
596 
597 	/* Continue next function in interrupt handler table */
598 	fm_irq_call_stage(fmdev, FM_RDS_START_IDX);
599 }
600 
601 static void fm_irq_handle_rds_start(struct fmdev *fmdev)
602 {
603 	if (fmdev->irq_info.flag & FM_RDS_EVENT & fmdev->irq_info.mask) {
604 		fmdbg("irq: rds threshold reached\n");
605 		fmdev->irq_info.stage = FM_RDS_SEND_RDS_GETCMD_IDX;
606 	} else {
607 		/* Continue next function in interrupt handler table */
608 		fmdev->irq_info.stage = FM_HW_TUNE_OP_ENDED_IDX;
609 	}
610 
611 	fm_irq_call(fmdev);
612 }
613 
614 static void fm_irq_send_rdsdata_getcmd(struct fmdev *fmdev)
615 {
616 	/* Send the command to read RDS data from the chip */
617 	if (!fm_send_cmd(fmdev, RDS_DATA_GET, REG_RD, NULL,
618 			    (FM_RX_RDS_FIFO_THRESHOLD * 3), NULL))
619 		fm_irq_timeout_stage(fmdev, FM_RDS_HANDLE_RDS_GETCMD_RESP_IDX);
620 }
621 
622 /* Keeps track of current RX channel AF (Alternate Frequency) */
623 static void fm_rx_update_af_cache(struct fmdev *fmdev, u8 af)
624 {
625 	struct tuned_station_info *stat_info = &fmdev->rx.stat_info;
626 	u8 reg_idx = fmdev->rx.region.fm_band;
627 	u8 index;
628 	u32 freq;
629 
630 	/* First AF indicates the number of AF follows. Reset the list */
631 	if ((af >= FM_RDS_1_AF_FOLLOWS) && (af <= FM_RDS_25_AF_FOLLOWS)) {
632 		fmdev->rx.stat_info.af_list_max = (af - FM_RDS_1_AF_FOLLOWS + 1);
633 		fmdev->rx.stat_info.afcache_size = 0;
634 		fmdbg("No of expected AF : %d\n", fmdev->rx.stat_info.af_list_max);
635 		return;
636 	}
637 
638 	if (af < FM_RDS_MIN_AF)
639 		return;
640 	if (reg_idx == FM_BAND_EUROPE_US && af > FM_RDS_MAX_AF)
641 		return;
642 	if (reg_idx == FM_BAND_JAPAN && af > FM_RDS_MAX_AF_JAPAN)
643 		return;
644 
645 	freq = fmdev->rx.region.bot_freq + (af * 100);
646 	if (freq == fmdev->rx.freq) {
647 		fmdbg("Current freq(%d) is matching with received AF(%d)\n",
648 				fmdev->rx.freq, freq);
649 		return;
650 	}
651 	/* Do check in AF cache */
652 	for (index = 0; index < stat_info->afcache_size; index++) {
653 		if (stat_info->af_cache[index] == freq)
654 			break;
655 	}
656 	/* Reached the limit of the list - ignore the next AF */
657 	if (index == stat_info->af_list_max) {
658 		fmdbg("AF cache is full\n");
659 		return;
660 	}
661 	/*
662 	 * If we reached the end of the list then this AF is not
663 	 * in the list - add it.
664 	 */
665 	if (index == stat_info->afcache_size) {
666 		fmdbg("Storing AF %d to cache index %d\n", freq, index);
667 		stat_info->af_cache[index] = freq;
668 		stat_info->afcache_size++;
669 	}
670 }
671 
672 /*
673  * Converts RDS buffer data from big endian format
674  * to little endian format.
675  */
676 static void fm_rdsparse_swapbytes(struct fmdev *fmdev,
677 		struct fm_rdsdata_format *rds_format)
678 {
679 	u8 index = 0;
680 	u8 *rds_buff;
681 
682 	/*
683 	 * Since in Orca the 2 RDS Data bytes are in little endian and
684 	 * in Dolphin they are in big endian, the parsing of the RDS data
685 	 * is chip dependent
686 	 */
687 	if (fmdev->asci_id != 0x6350) {
688 		rds_buff = &rds_format->data.groupdatabuff.buff[0];
689 		while (index + 1 < FM_RX_RDS_INFO_FIELD_MAX) {
690 			swap(rds_buff[index], rds_buff[index + 1]);
691 			index += 2;
692 		}
693 	}
694 }
695 
696 static void fm_irq_handle_rdsdata_getcmd_resp(struct fmdev *fmdev)
697 {
698 	struct sk_buff *skb;
699 	struct fm_rdsdata_format rds_fmt;
700 	struct fm_rds *rds = &fmdev->rx.rds;
701 	unsigned long group_idx, flags;
702 	u8 *rds_data, meta_data, tmpbuf[FM_RDS_BLK_SIZE];
703 	u8 type, blk_idx;
704 	u16 cur_picode;
705 	u32 rds_len;
706 
707 	if (check_cmdresp_status(fmdev, &skb))
708 		return;
709 
710 	/* Skip header info */
711 	skb_pull(skb, sizeof(struct fm_event_msg_hdr));
712 	rds_data = skb->data;
713 	rds_len = skb->len;
714 
715 	/* Parse the RDS data */
716 	while (rds_len >= FM_RDS_BLK_SIZE) {
717 		meta_data = rds_data[2];
718 		/* Get the type: 0=A, 1=B, 2=C, 3=C', 4=D, 5=E */
719 		type = (meta_data & 0x07);
720 
721 		/* Transform the blk type into index sequence (0, 1, 2, 3, 4) */
722 		blk_idx = (type <= FM_RDS_BLOCK_C ? type : (type - 1));
723 		fmdbg("Block index:%d(%s)\n", blk_idx,
724 			   (meta_data & FM_RDS_STATUS_ERR_MASK) ? "Bad" : "Ok");
725 
726 		if ((meta_data & FM_RDS_STATUS_ERR_MASK) != 0)
727 			break;
728 
729 		if (blk_idx > FM_RDS_BLK_IDX_D) {
730 			fmdbg("Block sequence mismatch\n");
731 			rds->last_blk_idx = -1;
732 			break;
733 		}
734 
735 		/* Skip checkword (control) byte and copy only data byte */
736 		memcpy(&rds_fmt.data.groupdatabuff.
737 				buff[blk_idx * (FM_RDS_BLK_SIZE - 1)],
738 				rds_data, (FM_RDS_BLK_SIZE - 1));
739 
740 		rds->last_blk_idx = blk_idx;
741 
742 		/* If completed a whole group then handle it */
743 		if (blk_idx == FM_RDS_BLK_IDX_D) {
744 			fmdbg("Good block received\n");
745 			fm_rdsparse_swapbytes(fmdev, &rds_fmt);
746 
747 			/*
748 			 * Extract PI code and store in local cache.
749 			 * We need this during AF switch processing.
750 			 */
751 			cur_picode = be16_to_cpu((__force __be16)rds_fmt.data.groupgeneral.pidata);
752 			if (fmdev->rx.stat_info.picode != cur_picode)
753 				fmdev->rx.stat_info.picode = cur_picode;
754 
755 			fmdbg("picode:%d\n", cur_picode);
756 
757 			group_idx = (rds_fmt.data.groupgeneral.blk_b[0] >> 3);
758 			fmdbg("(fmdrv):Group:%ld%s\n", group_idx/2,
759 					(group_idx % 2) ? "B" : "A");
760 
761 			group_idx = 1 << (rds_fmt.data.groupgeneral.blk_b[0] >> 3);
762 			if (group_idx == FM_RDS_GROUP_TYPE_MASK_0A) {
763 				fm_rx_update_af_cache(fmdev, rds_fmt.data.group0A.af[0]);
764 				fm_rx_update_af_cache(fmdev, rds_fmt.data.group0A.af[1]);
765 			}
766 		}
767 		rds_len -= FM_RDS_BLK_SIZE;
768 		rds_data += FM_RDS_BLK_SIZE;
769 	}
770 
771 	/* Copy raw rds data to internal rds buffer */
772 	rds_data = skb->data;
773 	rds_len = skb->len;
774 
775 	spin_lock_irqsave(&fmdev->rds_buff_lock, flags);
776 	while (rds_len > 0) {
777 		/*
778 		 * Fill RDS buffer as per V4L2 specification.
779 		 * Store control byte
780 		 */
781 		type = (rds_data[2] & 0x07);
782 		blk_idx = (type <= FM_RDS_BLOCK_C ? type : (type - 1));
783 		tmpbuf[2] = blk_idx;	/* Offset name */
784 		tmpbuf[2] |= blk_idx << 3;	/* Received offset */
785 
786 		/* Store data byte */
787 		tmpbuf[0] = rds_data[0];
788 		tmpbuf[1] = rds_data[1];
789 
790 		memcpy(&rds->buff[rds->wr_idx], &tmpbuf, FM_RDS_BLK_SIZE);
791 		rds->wr_idx = (rds->wr_idx + FM_RDS_BLK_SIZE) % rds->buf_size;
792 
793 		/* Check for overflow & start over */
794 		if (rds->wr_idx == rds->rd_idx) {
795 			fmdbg("RDS buffer overflow\n");
796 			rds->wr_idx = 0;
797 			rds->rd_idx = 0;
798 			break;
799 		}
800 		rds_len -= FM_RDS_BLK_SIZE;
801 		rds_data += FM_RDS_BLK_SIZE;
802 	}
803 	spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
804 
805 	/* Wakeup read queue */
806 	if (rds->wr_idx != rds->rd_idx)
807 		wake_up_interruptible(&rds->read_queue);
808 
809 	fm_irq_call_stage(fmdev, FM_RDS_FINISH_IDX);
810 }
811 
812 static void fm_irq_handle_rds_finish(struct fmdev *fmdev)
813 {
814 	fm_irq_call_stage(fmdev, FM_HW_TUNE_OP_ENDED_IDX);
815 }
816 
817 static void fm_irq_handle_tune_op_ended(struct fmdev *fmdev)
818 {
819 	if (fmdev->irq_info.flag & (FM_FR_EVENT | FM_BL_EVENT) & fmdev->
820 	    irq_info.mask) {
821 		fmdbg("irq: tune ended/bandlimit reached\n");
822 		if (test_and_clear_bit(FM_AF_SWITCH_INPROGRESS, &fmdev->flag)) {
823 			fmdev->irq_info.stage = FM_AF_JUMP_RD_FREQ_IDX;
824 		} else {
825 			complete(&fmdev->maintask_comp);
826 			fmdev->irq_info.stage = FM_HW_POWER_ENB_IDX;
827 		}
828 	} else
829 		fmdev->irq_info.stage = FM_HW_POWER_ENB_IDX;
830 
831 	fm_irq_call(fmdev);
832 }
833 
834 static void fm_irq_handle_power_enb(struct fmdev *fmdev)
835 {
836 	if (fmdev->irq_info.flag & FM_POW_ENB_EVENT) {
837 		fmdbg("irq: Power Enabled/Disabled\n");
838 		complete(&fmdev->maintask_comp);
839 	}
840 
841 	fm_irq_call_stage(fmdev, FM_LOW_RSSI_START_IDX);
842 }
843 
844 static void fm_irq_handle_low_rssi_start(struct fmdev *fmdev)
845 {
846 	if ((fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON) &&
847 	    (fmdev->irq_info.flag & FM_LEV_EVENT & fmdev->irq_info.mask) &&
848 	    (fmdev->rx.freq != FM_UNDEFINED_FREQ) &&
849 	    (fmdev->rx.stat_info.afcache_size != 0)) {
850 		fmdbg("irq: rssi level has fallen below threshold level\n");
851 
852 		/* Disable further low RSSI interrupts */
853 		fmdev->irq_info.mask &= ~FM_LEV_EVENT;
854 
855 		fmdev->rx.afjump_idx = 0;
856 		fmdev->rx.freq_before_jump = fmdev->rx.freq;
857 		fmdev->irq_info.stage = FM_AF_JUMP_SETPI_IDX;
858 	} else {
859 		/* Continue next function in interrupt handler table */
860 		fmdev->irq_info.stage = FM_SEND_INTMSK_CMD_IDX;
861 	}
862 
863 	fm_irq_call(fmdev);
864 }
865 
866 static void fm_irq_afjump_set_pi(struct fmdev *fmdev)
867 {
868 	u16 payload;
869 
870 	/* Set PI code - must be updated if the AF list is not empty */
871 	payload = fmdev->rx.stat_info.picode;
872 	if (!fm_send_cmd(fmdev, RDS_PI_SET, REG_WR, &payload, sizeof(payload), NULL))
873 		fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SETPI_RESP_IDX);
874 }
875 
876 static void fm_irq_handle_set_pi_resp(struct fmdev *fmdev)
877 {
878 	fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_SETPI_MASK_IDX);
879 }
880 
881 /*
882  * Set PI mask.
883  * 0xFFFF = Enable PI code matching
884  * 0x0000 = Disable PI code matching
885  */
886 static void fm_irq_afjump_set_pimask(struct fmdev *fmdev)
887 {
888 	u16 payload;
889 
890 	payload = 0x0000;
891 	if (!fm_send_cmd(fmdev, RDS_PI_MASK_SET, REG_WR, &payload, sizeof(payload), NULL))
892 		fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SETPI_MASK_RESP_IDX);
893 }
894 
895 static void fm_irq_handle_set_pimask_resp(struct fmdev *fmdev)
896 {
897 	fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_SET_AF_FREQ_IDX);
898 }
899 
900 static void fm_irq_afjump_setfreq(struct fmdev *fmdev)
901 {
902 	u16 frq_index;
903 	u16 payload;
904 
905 	fmdbg("Switch to %d KHz\n", fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx]);
906 	frq_index = (fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx] -
907 	     fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
908 
909 	payload = frq_index;
910 	if (!fm_send_cmd(fmdev, AF_FREQ_SET, REG_WR, &payload, sizeof(payload), NULL))
911 		fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SET_AFFREQ_RESP_IDX);
912 }
913 
914 static void fm_irq_handle_setfreq_resp(struct fmdev *fmdev)
915 {
916 	fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_ENABLE_INT_IDX);
917 }
918 
919 static void fm_irq_afjump_enableint(struct fmdev *fmdev)
920 {
921 	u16 payload;
922 
923 	/* Enable FR (tuning operation ended) interrupt */
924 	payload = FM_FR_EVENT;
925 	if (!fm_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload, sizeof(payload), NULL))
926 		fm_irq_timeout_stage(fmdev, FM_AF_JUMP_ENABLE_INT_RESP_IDX);
927 }
928 
929 static void fm_irq_afjump_enableint_resp(struct fmdev *fmdev)
930 {
931 	fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_START_AFJUMP_IDX);
932 }
933 
934 static void fm_irq_start_afjump(struct fmdev *fmdev)
935 {
936 	u16 payload;
937 
938 	payload = FM_TUNER_AF_JUMP_MODE;
939 	if (!fm_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
940 			sizeof(payload), NULL))
941 		fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_START_AFJUMP_RESP_IDX);
942 }
943 
944 static void fm_irq_handle_start_afjump_resp(struct fmdev *fmdev)
945 {
946 	struct sk_buff *skb;
947 
948 	if (check_cmdresp_status(fmdev, &skb))
949 		return;
950 
951 	fmdev->irq_info.stage = FM_SEND_FLAG_GETCMD_IDX;
952 	set_bit(FM_AF_SWITCH_INPROGRESS, &fmdev->flag);
953 	clear_bit(FM_INTTASK_RUNNING, &fmdev->flag);
954 }
955 
956 static void fm_irq_afjump_rd_freq(struct fmdev *fmdev)
957 {
958 	u16 payload;
959 
960 	if (!fm_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, sizeof(payload), NULL))
961 		fm_irq_timeout_stage(fmdev, FM_AF_JUMP_RD_FREQ_RESP_IDX);
962 }
963 
964 static void fm_irq_afjump_rd_freq_resp(struct fmdev *fmdev)
965 {
966 	struct sk_buff *skb;
967 	u16 read_freq;
968 	u32 curr_freq, jumped_freq;
969 
970 	if (check_cmdresp_status(fmdev, &skb))
971 		return;
972 
973 	/* Skip header info and copy only response data */
974 	skb_pull(skb, sizeof(struct fm_event_msg_hdr));
975 	memcpy(&read_freq, skb->data, sizeof(read_freq));
976 	read_freq = be16_to_cpu((__force __be16)read_freq);
977 	curr_freq = fmdev->rx.region.bot_freq + ((u32)read_freq * FM_FREQ_MUL);
978 
979 	jumped_freq = fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx];
980 
981 	/* If the frequency was changed the jump succeeded */
982 	if ((curr_freq != fmdev->rx.freq_before_jump) && (curr_freq == jumped_freq)) {
983 		fmdbg("Successfully switched to alternate freq %d\n", curr_freq);
984 		fmdev->rx.freq = curr_freq;
985 		fm_rx_reset_rds_cache(fmdev);
986 
987 		/* AF feature is on, enable low level RSSI interrupt */
988 		if (fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
989 			fmdev->irq_info.mask |= FM_LEV_EVENT;
990 
991 		fmdev->irq_info.stage = FM_LOW_RSSI_FINISH_IDX;
992 	} else {		/* jump to the next freq in the AF list */
993 		fmdev->rx.afjump_idx++;
994 
995 		/* If we reached the end of the list - stop searching */
996 		if (fmdev->rx.afjump_idx >= fmdev->rx.stat_info.afcache_size) {
997 			fmdbg("AF switch processing failed\n");
998 			fmdev->irq_info.stage = FM_LOW_RSSI_FINISH_IDX;
999 		} else {	/* AF List is not over - try next one */
1000 
1001 			fmdbg("Trying next freq in AF cache\n");
1002 			fmdev->irq_info.stage = FM_AF_JUMP_SETPI_IDX;
1003 		}
1004 	}
1005 	fm_irq_call(fmdev);
1006 }
1007 
1008 static void fm_irq_handle_low_rssi_finish(struct fmdev *fmdev)
1009 {
1010 	fm_irq_call_stage(fmdev, FM_SEND_INTMSK_CMD_IDX);
1011 }
1012 
1013 static void fm_irq_send_intmsk_cmd(struct fmdev *fmdev)
1014 {
1015 	u16 payload;
1016 
1017 	/* Re-enable FM interrupts */
1018 	payload = fmdev->irq_info.mask;
1019 
1020 	if (!fm_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
1021 			sizeof(payload), NULL))
1022 		fm_irq_timeout_stage(fmdev, FM_HANDLE_INTMSK_CMD_RESP_IDX);
1023 }
1024 
1025 static void fm_irq_handle_intmsk_cmd_resp(struct fmdev *fmdev)
1026 {
1027 	struct sk_buff *skb;
1028 
1029 	if (check_cmdresp_status(fmdev, &skb))
1030 		return;
1031 	/*
1032 	 * This is last function in interrupt table to be executed.
1033 	 * So, reset stage index to 0.
1034 	 */
1035 	fmdev->irq_info.stage = FM_SEND_FLAG_GETCMD_IDX;
1036 
1037 	/* Start processing any pending interrupt */
1038 	if (test_and_clear_bit(FM_INTTASK_SCHEDULE_PENDING, &fmdev->flag))
1039 		fmdev->irq_info.handlers[fmdev->irq_info.stage](fmdev);
1040 	else
1041 		clear_bit(FM_INTTASK_RUNNING, &fmdev->flag);
1042 }
1043 
1044 /* Returns availability of RDS data in internal buffer */
1045 int fmc_is_rds_data_available(struct fmdev *fmdev, struct file *file,
1046 				struct poll_table_struct *pts)
1047 {
1048 	poll_wait(file, &fmdev->rx.rds.read_queue, pts);
1049 	if (fmdev->rx.rds.rd_idx != fmdev->rx.rds.wr_idx)
1050 		return 0;
1051 
1052 	return -EAGAIN;
1053 }
1054 
1055 /* Copies RDS data from internal buffer to user buffer */
1056 int fmc_transfer_rds_from_internal_buff(struct fmdev *fmdev, struct file *file,
1057 		u8 __user *buf, size_t count)
1058 {
1059 	u32 block_count;
1060 	u8 tmpbuf[FM_RDS_BLK_SIZE];
1061 	unsigned long flags;
1062 	int ret;
1063 
1064 	if (fmdev->rx.rds.wr_idx == fmdev->rx.rds.rd_idx) {
1065 		if (file->f_flags & O_NONBLOCK)
1066 			return -EWOULDBLOCK;
1067 
1068 		ret = wait_event_interruptible(fmdev->rx.rds.read_queue,
1069 				(fmdev->rx.rds.wr_idx != fmdev->rx.rds.rd_idx));
1070 		if (ret)
1071 			return -EINTR;
1072 	}
1073 
1074 	/* Calculate block count from byte count */
1075 	count /= FM_RDS_BLK_SIZE;
1076 	block_count = 0;
1077 	ret = 0;
1078 
1079 	while (block_count < count) {
1080 		spin_lock_irqsave(&fmdev->rds_buff_lock, flags);
1081 
1082 		if (fmdev->rx.rds.wr_idx == fmdev->rx.rds.rd_idx) {
1083 			spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
1084 			break;
1085 		}
1086 		memcpy(tmpbuf, &fmdev->rx.rds.buff[fmdev->rx.rds.rd_idx],
1087 					FM_RDS_BLK_SIZE);
1088 		fmdev->rx.rds.rd_idx += FM_RDS_BLK_SIZE;
1089 		if (fmdev->rx.rds.rd_idx >= fmdev->rx.rds.buf_size)
1090 			fmdev->rx.rds.rd_idx = 0;
1091 
1092 		spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
1093 
1094 		if (copy_to_user(buf, tmpbuf, FM_RDS_BLK_SIZE))
1095 			break;
1096 
1097 		block_count++;
1098 		buf += FM_RDS_BLK_SIZE;
1099 		ret += FM_RDS_BLK_SIZE;
1100 	}
1101 	return ret;
1102 }
1103 
1104 int fmc_set_freq(struct fmdev *fmdev, u32 freq_to_set)
1105 {
1106 	switch (fmdev->curr_fmmode) {
1107 	case FM_MODE_RX:
1108 		return fm_rx_set_freq(fmdev, freq_to_set);
1109 
1110 	case FM_MODE_TX:
1111 		return fm_tx_set_freq(fmdev, freq_to_set);
1112 
1113 	default:
1114 		return -EINVAL;
1115 	}
1116 }
1117 
1118 int fmc_get_freq(struct fmdev *fmdev, u32 *cur_tuned_frq)
1119 {
1120 	if (fmdev->rx.freq == FM_UNDEFINED_FREQ) {
1121 		fmerr("RX frequency is not set\n");
1122 		return -EPERM;
1123 	}
1124 	if (cur_tuned_frq == NULL) {
1125 		fmerr("Invalid memory\n");
1126 		return -ENOMEM;
1127 	}
1128 
1129 	switch (fmdev->curr_fmmode) {
1130 	case FM_MODE_RX:
1131 		*cur_tuned_frq = fmdev->rx.freq;
1132 		return 0;
1133 
1134 	case FM_MODE_TX:
1135 		*cur_tuned_frq = 0;	/* TODO : Change this later */
1136 		return 0;
1137 
1138 	default:
1139 		return -EINVAL;
1140 	}
1141 
1142 }
1143 
1144 int fmc_set_region(struct fmdev *fmdev, u8 region_to_set)
1145 {
1146 	switch (fmdev->curr_fmmode) {
1147 	case FM_MODE_RX:
1148 		return fm_rx_set_region(fmdev, region_to_set);
1149 
1150 	case FM_MODE_TX:
1151 		return fm_tx_set_region(fmdev, region_to_set);
1152 
1153 	default:
1154 		return -EINVAL;
1155 	}
1156 }
1157 
1158 int fmc_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
1159 {
1160 	switch (fmdev->curr_fmmode) {
1161 	case FM_MODE_RX:
1162 		return fm_rx_set_mute_mode(fmdev, mute_mode_toset);
1163 
1164 	case FM_MODE_TX:
1165 		return fm_tx_set_mute_mode(fmdev, mute_mode_toset);
1166 
1167 	default:
1168 		return -EINVAL;
1169 	}
1170 }
1171 
1172 int fmc_set_stereo_mono(struct fmdev *fmdev, u16 mode)
1173 {
1174 	switch (fmdev->curr_fmmode) {
1175 	case FM_MODE_RX:
1176 		return fm_rx_set_stereo_mono(fmdev, mode);
1177 
1178 	case FM_MODE_TX:
1179 		return fm_tx_set_stereo_mono(fmdev, mode);
1180 
1181 	default:
1182 		return -EINVAL;
1183 	}
1184 }
1185 
1186 int fmc_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
1187 {
1188 	switch (fmdev->curr_fmmode) {
1189 	case FM_MODE_RX:
1190 		return fm_rx_set_rds_mode(fmdev, rds_en_dis);
1191 
1192 	case FM_MODE_TX:
1193 		return fm_tx_set_rds_mode(fmdev, rds_en_dis);
1194 
1195 	default:
1196 		return -EINVAL;
1197 	}
1198 }
1199 
1200 /* Sends power off command to the chip */
1201 static int fm_power_down(struct fmdev *fmdev)
1202 {
1203 	u16 payload;
1204 	int ret;
1205 
1206 	if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
1207 		fmerr("FM core is not ready\n");
1208 		return -EPERM;
1209 	}
1210 	if (fmdev->curr_fmmode == FM_MODE_OFF) {
1211 		fmdbg("FM chip is already in OFF state\n");
1212 		return 0;
1213 	}
1214 
1215 	payload = 0x0;
1216 	ret = fmc_send_cmd(fmdev, FM_POWER_MODE, REG_WR, &payload,
1217 		sizeof(payload), NULL, NULL);
1218 	if (ret < 0)
1219 		return ret;
1220 
1221 	return fmc_release(fmdev);
1222 }
1223 
1224 /* Reads init command from FM firmware file and loads to the chip */
1225 static int fm_download_firmware(struct fmdev *fmdev, const u8 *fw_name)
1226 {
1227 	const struct firmware *fw_entry;
1228 	struct bts_header *fw_header;
1229 	struct bts_action *action;
1230 	struct bts_action_delay *delay;
1231 	u8 *fw_data;
1232 	int ret, fw_len, cmd_cnt;
1233 
1234 	cmd_cnt = 0;
1235 	set_bit(FM_FW_DW_INPROGRESS, &fmdev->flag);
1236 
1237 	ret = request_firmware(&fw_entry, fw_name,
1238 				&fmdev->radio_dev->dev);
1239 	if (ret < 0) {
1240 		fmerr("Unable to read firmware(%s) content\n", fw_name);
1241 		return ret;
1242 	}
1243 	fmdbg("Firmware(%s) length : %zu bytes\n", fw_name, fw_entry->size);
1244 
1245 	fw_data = (void *)fw_entry->data;
1246 	fw_len = fw_entry->size;
1247 
1248 	fw_header = (struct bts_header *)fw_data;
1249 	if (fw_header->magic != FM_FW_FILE_HEADER_MAGIC) {
1250 		fmerr("%s not a legal TI firmware file\n", fw_name);
1251 		ret = -EINVAL;
1252 		goto rel_fw;
1253 	}
1254 	fmdbg("FW(%s) magic number : 0x%x\n", fw_name, fw_header->magic);
1255 
1256 	/* Skip file header info , we already verified it */
1257 	fw_data += sizeof(struct bts_header);
1258 	fw_len -= sizeof(struct bts_header);
1259 
1260 	while (fw_data && fw_len > 0) {
1261 		action = (struct bts_action *)fw_data;
1262 
1263 		switch (action->type) {
1264 		case ACTION_SEND_COMMAND:	/* Send */
1265 			ret = fmc_send_cmd(fmdev, 0, 0, action->data,
1266 					   action->size, NULL, NULL);
1267 			if (ret)
1268 				goto rel_fw;
1269 
1270 			cmd_cnt++;
1271 			break;
1272 
1273 		case ACTION_DELAY:	/* Delay */
1274 			delay = (struct bts_action_delay *)action->data;
1275 			mdelay(delay->msec);
1276 			break;
1277 		}
1278 
1279 		fw_data += (sizeof(struct bts_action) + (action->size));
1280 		fw_len -= (sizeof(struct bts_action) + (action->size));
1281 	}
1282 	fmdbg("Firmware commands(%d) loaded to chip\n", cmd_cnt);
1283 rel_fw:
1284 	release_firmware(fw_entry);
1285 	clear_bit(FM_FW_DW_INPROGRESS, &fmdev->flag);
1286 
1287 	return ret;
1288 }
1289 
1290 /* Loads default RX configuration to the chip */
1291 static int load_default_rx_configuration(struct fmdev *fmdev)
1292 {
1293 	int ret;
1294 
1295 	ret = fm_rx_set_volume(fmdev, FM_DEFAULT_RX_VOLUME);
1296 	if (ret < 0)
1297 		return ret;
1298 
1299 	return fm_rx_set_rssi_threshold(fmdev, FM_DEFAULT_RSSI_THRESHOLD);
1300 }
1301 
1302 /* Does FM power on sequence */
1303 static int fm_power_up(struct fmdev *fmdev, u8 mode)
1304 {
1305 	u16 payload;
1306 	__be16 asic_id = 0, asic_ver = 0;
1307 	int resp_len, ret;
1308 	u8 fw_name[50];
1309 
1310 	if (mode >= FM_MODE_ENTRY_MAX) {
1311 		fmerr("Invalid firmware download option\n");
1312 		return -EINVAL;
1313 	}
1314 
1315 	/*
1316 	 * Initialize FM common module. FM GPIO toggling is
1317 	 * taken care in Shared Transport driver.
1318 	 */
1319 	ret = fmc_prepare(fmdev);
1320 	if (ret < 0) {
1321 		fmerr("Unable to prepare FM Common\n");
1322 		return ret;
1323 	}
1324 
1325 	payload = FM_ENABLE;
1326 	if (fmc_send_cmd(fmdev, FM_POWER_MODE, REG_WR, &payload,
1327 			sizeof(payload), NULL, NULL))
1328 		goto rel;
1329 
1330 	/* Allow the chip to settle down in Channel-8 mode */
1331 	msleep(20);
1332 
1333 	if (fmc_send_cmd(fmdev, ASIC_ID_GET, REG_RD, NULL,
1334 			sizeof(asic_id), &asic_id, &resp_len))
1335 		goto rel;
1336 
1337 	if (fmc_send_cmd(fmdev, ASIC_VER_GET, REG_RD, NULL,
1338 			sizeof(asic_ver), &asic_ver, &resp_len))
1339 		goto rel;
1340 
1341 	fmdbg("ASIC ID: 0x%x , ASIC Version: %d\n",
1342 		be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
1343 
1344 	sprintf(fw_name, "%s_%x.%d.bts", FM_FMC_FW_FILE_START,
1345 		be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
1346 
1347 	ret = fm_download_firmware(fmdev, fw_name);
1348 	if (ret < 0) {
1349 		fmdbg("Failed to download firmware file %s\n", fw_name);
1350 		goto rel;
1351 	}
1352 	sprintf(fw_name, "%s_%x.%d.bts", (mode == FM_MODE_RX) ?
1353 			FM_RX_FW_FILE_START : FM_TX_FW_FILE_START,
1354 			be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
1355 
1356 	ret = fm_download_firmware(fmdev, fw_name);
1357 	if (ret < 0) {
1358 		fmdbg("Failed to download firmware file %s\n", fw_name);
1359 		goto rel;
1360 	} else
1361 		return ret;
1362 rel:
1363 	return fmc_release(fmdev);
1364 }
1365 
1366 /* Set FM Modes(TX, RX, OFF) */
1367 int fmc_set_mode(struct fmdev *fmdev, u8 fm_mode)
1368 {
1369 	int ret = 0;
1370 
1371 	if (fm_mode >= FM_MODE_ENTRY_MAX) {
1372 		fmerr("Invalid FM mode\n");
1373 		return -EINVAL;
1374 	}
1375 	if (fmdev->curr_fmmode == fm_mode) {
1376 		fmdbg("Already fm is in mode(%d)\n", fm_mode);
1377 		return ret;
1378 	}
1379 
1380 	switch (fm_mode) {
1381 	case FM_MODE_OFF:	/* OFF Mode */
1382 		ret = fm_power_down(fmdev);
1383 		if (ret < 0) {
1384 			fmerr("Failed to set OFF mode\n");
1385 			return ret;
1386 		}
1387 		break;
1388 
1389 	case FM_MODE_TX:	/* TX Mode */
1390 	case FM_MODE_RX:	/* RX Mode */
1391 		/* Power down before switching to TX or RX mode */
1392 		if (fmdev->curr_fmmode != FM_MODE_OFF) {
1393 			ret = fm_power_down(fmdev);
1394 			if (ret < 0) {
1395 				fmerr("Failed to set OFF mode\n");
1396 				return ret;
1397 			}
1398 			msleep(30);
1399 		}
1400 		ret = fm_power_up(fmdev, fm_mode);
1401 		if (ret < 0) {
1402 			fmerr("Failed to load firmware\n");
1403 			return ret;
1404 		}
1405 	}
1406 	fmdev->curr_fmmode = fm_mode;
1407 
1408 	/* Set default configuration */
1409 	if (fmdev->curr_fmmode == FM_MODE_RX) {
1410 		fmdbg("Loading default rx configuration..\n");
1411 		ret = load_default_rx_configuration(fmdev);
1412 		if (ret < 0)
1413 			fmerr("Failed to load default values\n");
1414 	}
1415 
1416 	return ret;
1417 }
1418 
1419 /* Returns current FM mode (TX, RX, OFF) */
1420 int fmc_get_mode(struct fmdev *fmdev, u8 *fmmode)
1421 {
1422 	if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
1423 		fmerr("FM core is not ready\n");
1424 		return -EPERM;
1425 	}
1426 	if (fmmode == NULL) {
1427 		fmerr("Invalid memory\n");
1428 		return -ENOMEM;
1429 	}
1430 
1431 	*fmmode = fmdev->curr_fmmode;
1432 	return 0;
1433 }
1434 
1435 /* Called by ST layer when FM packet is available */
1436 static long fm_st_receive(void *arg, struct sk_buff *skb)
1437 {
1438 	struct fmdev *fmdev;
1439 
1440 	fmdev = (struct fmdev *)arg;
1441 
1442 	if (skb == NULL) {
1443 		fmerr("Invalid SKB received from ST\n");
1444 		return -EFAULT;
1445 	}
1446 
1447 	if (skb->cb[0] != FM_PKT_LOGICAL_CHAN_NUMBER) {
1448 		fmerr("Received SKB (%p) is not FM Channel 8 pkt\n", skb);
1449 		return -EINVAL;
1450 	}
1451 
1452 	memcpy(skb_push(skb, 1), &skb->cb[0], 1);
1453 	skb_queue_tail(&fmdev->rx_q, skb);
1454 	tasklet_schedule(&fmdev->rx_task);
1455 
1456 	return 0;
1457 }
1458 
1459 /*
1460  * Called by ST layer to indicate protocol registration completion
1461  * status.
1462  */
1463 static void fm_st_reg_comp_cb(void *arg, int data)
1464 {
1465 	struct fmdev *fmdev;
1466 
1467 	fmdev = (struct fmdev *)arg;
1468 	fmdev->streg_cbdata = data;
1469 	complete(&wait_for_fmdrv_reg_comp);
1470 }
1471 
1472 /*
1473  * This function will be called from FM V4L2 open function.
1474  * Register with ST driver and initialize driver data.
1475  */
1476 int fmc_prepare(struct fmdev *fmdev)
1477 {
1478 	static struct st_proto_s fm_st_proto;
1479 	int ret;
1480 
1481 	if (test_bit(FM_CORE_READY, &fmdev->flag)) {
1482 		fmdbg("FM Core is already up\n");
1483 		return 0;
1484 	}
1485 
1486 	memset(&fm_st_proto, 0, sizeof(fm_st_proto));
1487 	fm_st_proto.recv = fm_st_receive;
1488 	fm_st_proto.match_packet = NULL;
1489 	fm_st_proto.reg_complete_cb = fm_st_reg_comp_cb;
1490 	fm_st_proto.write = NULL; /* TI ST driver will fill write pointer */
1491 	fm_st_proto.priv_data = fmdev;
1492 	fm_st_proto.chnl_id = 0x08;
1493 	fm_st_proto.max_frame_size = 0xff;
1494 	fm_st_proto.hdr_len = 1;
1495 	fm_st_proto.offset_len_in_hdr = 0;
1496 	fm_st_proto.len_size = 1;
1497 	fm_st_proto.reserve = 1;
1498 
1499 	ret = st_register(&fm_st_proto);
1500 	if (ret == -EINPROGRESS) {
1501 		init_completion(&wait_for_fmdrv_reg_comp);
1502 		fmdev->streg_cbdata = -EINPROGRESS;
1503 		fmdbg("%s waiting for ST reg completion signal\n", __func__);
1504 
1505 		if (!wait_for_completion_timeout(&wait_for_fmdrv_reg_comp,
1506 						 FM_ST_REG_TIMEOUT)) {
1507 			fmerr("Timeout(%d sec), didn't get reg completion signal from ST\n",
1508 					jiffies_to_msecs(FM_ST_REG_TIMEOUT) / 1000);
1509 			return -ETIMEDOUT;
1510 		}
1511 		if (fmdev->streg_cbdata != 0) {
1512 			fmerr("ST reg comp CB called with error status %d\n",
1513 			      fmdev->streg_cbdata);
1514 			return -EAGAIN;
1515 		}
1516 
1517 		ret = 0;
1518 	} else if (ret < 0) {
1519 		fmerr("st_register failed %d\n", ret);
1520 		return -EAGAIN;
1521 	}
1522 
1523 	if (fm_st_proto.write != NULL) {
1524 		g_st_write = fm_st_proto.write;
1525 	} else {
1526 		fmerr("Failed to get ST write func pointer\n");
1527 		ret = st_unregister(&fm_st_proto);
1528 		if (ret < 0)
1529 			fmerr("st_unregister failed %d\n", ret);
1530 		return -EAGAIN;
1531 	}
1532 
1533 	spin_lock_init(&fmdev->rds_buff_lock);
1534 	spin_lock_init(&fmdev->resp_skb_lock);
1535 
1536 	/* Initialize TX queue and TX tasklet */
1537 	skb_queue_head_init(&fmdev->tx_q);
1538 	tasklet_setup(&fmdev->tx_task, send_tasklet);
1539 
1540 	/* Initialize RX Queue and RX tasklet */
1541 	skb_queue_head_init(&fmdev->rx_q);
1542 	tasklet_setup(&fmdev->rx_task, recv_tasklet);
1543 
1544 	fmdev->irq_info.stage = 0;
1545 	atomic_set(&fmdev->tx_cnt, 1);
1546 	fmdev->resp_comp = NULL;
1547 
1548 	timer_setup(&fmdev->irq_info.timer, int_timeout_handler, 0);
1549 	/*TODO: add FM_STIC_EVENT later */
1550 	fmdev->irq_info.mask = FM_MAL_EVENT;
1551 
1552 	/* Region info */
1553 	fmdev->rx.region = region_configs[default_radio_region];
1554 
1555 	fmdev->rx.mute_mode = FM_MUTE_OFF;
1556 	fmdev->rx.rf_depend_mute = FM_RX_RF_DEPENDENT_MUTE_OFF;
1557 	fmdev->rx.rds.flag = FM_RDS_DISABLE;
1558 	fmdev->rx.freq = FM_UNDEFINED_FREQ;
1559 	fmdev->rx.rds_mode = FM_RDS_SYSTEM_RDS;
1560 	fmdev->rx.af_mode = FM_RX_RDS_AF_SWITCH_MODE_OFF;
1561 	fmdev->irq_info.retry = 0;
1562 
1563 	fm_rx_reset_rds_cache(fmdev);
1564 	init_waitqueue_head(&fmdev->rx.rds.read_queue);
1565 
1566 	fm_rx_reset_station_info(fmdev);
1567 	set_bit(FM_CORE_READY, &fmdev->flag);
1568 
1569 	return ret;
1570 }
1571 
1572 /*
1573  * This function will be called from FM V4L2 release function.
1574  * Unregister from ST driver.
1575  */
1576 int fmc_release(struct fmdev *fmdev)
1577 {
1578 	static struct st_proto_s fm_st_proto;
1579 	int ret;
1580 
1581 	if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
1582 		fmdbg("FM Core is already down\n");
1583 		return 0;
1584 	}
1585 	/* Service pending read */
1586 	wake_up_interruptible(&fmdev->rx.rds.read_queue);
1587 
1588 	tasklet_kill(&fmdev->tx_task);
1589 	tasklet_kill(&fmdev->rx_task);
1590 
1591 	skb_queue_purge(&fmdev->tx_q);
1592 	skb_queue_purge(&fmdev->rx_q);
1593 
1594 	fmdev->resp_comp = NULL;
1595 	fmdev->rx.freq = 0;
1596 
1597 	memset(&fm_st_proto, 0, sizeof(fm_st_proto));
1598 	fm_st_proto.chnl_id = 0x08;
1599 
1600 	ret = st_unregister(&fm_st_proto);
1601 
1602 	if (ret < 0)
1603 		fmerr("Failed to de-register FM from ST %d\n", ret);
1604 	else
1605 		fmdbg("Successfully unregistered from ST\n");
1606 
1607 	clear_bit(FM_CORE_READY, &fmdev->flag);
1608 	return ret;
1609 }
1610 
1611 /*
1612  * Module init function. Ask FM V4L module to register video device.
1613  * Allocate memory for FM driver context and RX RDS buffer.
1614  */
1615 static int __init fm_drv_init(void)
1616 {
1617 	struct fmdev *fmdev = NULL;
1618 	int ret = -ENOMEM;
1619 
1620 	fmdbg("FM driver version %s\n", FM_DRV_VERSION);
1621 
1622 	fmdev = kzalloc(sizeof(struct fmdev), GFP_KERNEL);
1623 	if (NULL == fmdev) {
1624 		fmerr("Can't allocate operation structure memory\n");
1625 		return ret;
1626 	}
1627 	fmdev->rx.rds.buf_size = default_rds_buf * FM_RDS_BLK_SIZE;
1628 	fmdev->rx.rds.buff = kzalloc(fmdev->rx.rds.buf_size, GFP_KERNEL);
1629 	if (NULL == fmdev->rx.rds.buff) {
1630 		fmerr("Can't allocate rds ring buffer\n");
1631 		goto rel_dev;
1632 	}
1633 
1634 	ret = fm_v4l2_init_video_device(fmdev, radio_nr);
1635 	if (ret < 0)
1636 		goto rel_rdsbuf;
1637 
1638 	fmdev->irq_info.handlers = int_handler_table;
1639 	fmdev->curr_fmmode = FM_MODE_OFF;
1640 	fmdev->tx_data.pwr_lvl = FM_PWR_LVL_DEF;
1641 	fmdev->tx_data.preemph = FM_TX_PREEMPH_50US;
1642 	return ret;
1643 
1644 rel_rdsbuf:
1645 	kfree(fmdev->rx.rds.buff);
1646 rel_dev:
1647 	kfree(fmdev);
1648 
1649 	return ret;
1650 }
1651 
1652 /* Module exit function. Ask FM V4L module to unregister video device */
1653 static void __exit fm_drv_exit(void)
1654 {
1655 	struct fmdev *fmdev = NULL;
1656 
1657 	fmdev = fm_v4l2_deinit_video_device();
1658 	if (fmdev != NULL) {
1659 		kfree(fmdev->rx.rds.buff);
1660 		kfree(fmdev);
1661 	}
1662 }
1663 
1664 module_init(fm_drv_init);
1665 module_exit(fm_drv_exit);
1666 
1667 /* ------------- Module Info ------------- */
1668 MODULE_AUTHOR("Manjunatha Halli <manjunatha_halli@ti.com>");
1669 MODULE_DESCRIPTION("FM Driver for TI's Connectivity chip. " FM_DRV_VERSION);
1670 MODULE_VERSION(FM_DRV_VERSION);
1671 MODULE_LICENSE("GPL");
1672