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