Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * Tuner "middleware" for Philips TEA5767 chip |
| 10 | * |
Nicolas Pennequin | 357ffb3 | 2008-05-05 10:32:46 +0000 | [diff] [blame] | 11 | * Copyright (C) 2004 Jörg Hohensohn |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 12 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version 2 |
| 16 | * of the License, or (at your option) any later version. |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ****************************************************************************/ |
Linus Nielsen Feltzing | adec21a | 2005-08-13 17:34:10 +0000 | [diff] [blame] | 22 | #include "config.h" |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 23 | #include <stdbool.h> |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 24 | #include <string.h> |
Jörg Hohensohn | ef8d508 | 2004-10-19 08:20:38 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include "kernel.h" |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 27 | #include "tuner.h" /* tuner abstraction interface */ |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 28 | #include "fmradio.h" |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 29 | #include "fmradio_i2c.h" /* physical interface driver */ |
| 30 | |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 31 | #define I2C_ADR 0xC0 |
Dominik Riebeling | 76b2efb | 2006-10-23 19:48:42 +0000 | [diff] [blame] | 32 | static unsigned char write_bytes[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 }; |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 33 | |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 34 | static void tea5767_set_clear(int byte, unsigned char bits, int set) |
| 35 | { |
| 36 | write_bytes[byte] &= ~bits; |
| 37 | if (set) |
| 38 | write_bytes[byte] |= bits; |
| 39 | } |
| 40 | |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 41 | /* tuner abstraction layer: set something to the tuner */ |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 42 | int tea5767_set(int setting, int value) |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 43 | { |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 44 | switch(setting) |
| 45 | { |
Jörg Hohensohn | ef8d508 | 2004-10-19 08:20:38 +0000 | [diff] [blame] | 46 | case RADIO_SLEEP: |
| 47 | /* init values */ |
Dominik Riebeling | 76b2efb | 2006-10-23 19:48:42 +0000 | [diff] [blame] | 48 | write_bytes[0] |= (1<<7); /* mute */ |
Jens Arnold | 524b85f | 2005-09-24 09:06:31 +0000 | [diff] [blame] | 49 | #if CONFIG_TUNER_XTAL == 32768 |
Dominik Riebeling | 76b2efb | 2006-10-23 19:48:42 +0000 | [diff] [blame] | 50 | /* 32.768kHz, soft mute, stereo noise cancelling */ |
| 51 | write_bytes[3] |= (1<<4) | (1<<3) | (1<<1); |
Linus Nielsen Feltzing | adec21a | 2005-08-13 17:34:10 +0000 | [diff] [blame] | 52 | #else |
Dominik Riebeling | 76b2efb | 2006-10-23 19:48:42 +0000 | [diff] [blame] | 53 | /* soft mute, stereo noise cancelling */ |
| 54 | write_bytes[3] |= (1<<3) | (1<<1); |
Linus Nielsen Feltzing | adec21a | 2005-08-13 17:34:10 +0000 | [diff] [blame] | 55 | #endif |
Dominik Riebeling | 76b2efb | 2006-10-23 19:48:42 +0000 | [diff] [blame] | 56 | /* sleep / standby mode */ |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 57 | tea5767_set_clear(3, (1<<6), value); |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 58 | break; |
| 59 | |
| 60 | case RADIO_FREQUENCY: |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 61 | { |
| 62 | int n; |
Jens Arnold | 524b85f | 2005-09-24 09:06:31 +0000 | [diff] [blame] | 63 | #if CONFIG_TUNER_XTAL == 32768 |
| 64 | n = (4 * (value - 225000) + 16384) / 32768; |
Linus Nielsen Feltzing | adec21a | 2005-08-13 17:34:10 +0000 | [diff] [blame] | 65 | #else |
Linus Nielsen Feltzing | ae1241b | 2005-08-24 09:15:13 +0000 | [diff] [blame] | 66 | n = (4 * (value - 225000)) / 50000; |
Linus Nielsen Feltzing | adec21a | 2005-08-13 17:34:10 +0000 | [diff] [blame] | 67 | #endif |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 68 | write_bytes[0] = (write_bytes[0] & 0xC0) | (n >> 8); |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 69 | write_bytes[1] = n; |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 70 | } |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 71 | break; |
| 72 | |
Michael Sevakis | a2ee6a6 | 2007-03-16 14:40:40 +0000 | [diff] [blame] | 73 | case RADIO_SCAN_FREQUENCY: |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 74 | tea5767_set(RADIO_FREQUENCY, value); |
Michael Sevakis | a2ee6a6 | 2007-03-16 14:40:40 +0000 | [diff] [blame] | 75 | sleep(HZ/30); |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 76 | return tea5767_get(RADIO_TUNED); |
Michael Sevakis | a2ee6a6 | 2007-03-16 14:40:40 +0000 | [diff] [blame] | 77 | |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 78 | case RADIO_MUTE: |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 79 | tea5767_set_clear(0, 0x80, value); |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 80 | break; |
| 81 | |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 82 | case RADIO_REGION: |
| 83 | { |
| 84 | const struct tea5767_region_data *rd = |
| 85 | &tea5767_region_data[value]; |
| 86 | |
| 87 | tea5767_set_clear(4, (1<<6), rd->deemphasis); |
| 88 | tea5767_set_clear(3, (1<<5), rd->band); |
| 89 | break; |
| 90 | } |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 91 | case RADIO_FORCE_MONO: |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 92 | tea5767_set_clear(2, 0x08, value); |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 93 | break; |
Jörg Hohensohn | 6694212 | 2004-10-18 07:58:59 +0000 | [diff] [blame] | 94 | default: |
Michael Sevakis | a2ee6a6 | 2007-03-16 14:40:40 +0000 | [diff] [blame] | 95 | return -1; |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 96 | } |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 97 | |
Jörg Hohensohn | 6694212 | 2004-10-18 07:58:59 +0000 | [diff] [blame] | 98 | fmradio_i2c_write(I2C_ADR, write_bytes, sizeof(write_bytes)); |
Michael Sevakis | a2ee6a6 | 2007-03-16 14:40:40 +0000 | [diff] [blame] | 99 | return 1; |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* tuner abstraction layer: read something from the tuner */ |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 103 | int tea5767_get(int setting) |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 104 | { |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 105 | unsigned char read_bytes[5]; |
Jörg Hohensohn | 6694212 | 2004-10-18 07:58:59 +0000 | [diff] [blame] | 106 | int val = -1; /* default for unsupported query */ |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 107 | |
| 108 | fmradio_i2c_read(I2C_ADR, read_bytes, sizeof(read_bytes)); |
| 109 | |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 110 | switch(setting) |
| 111 | { |
| 112 | case RADIO_PRESENT: |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 113 | val = 1; /* true */ |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 114 | break; |
| 115 | |
Jörg Hohensohn | ef8d508 | 2004-10-19 08:20:38 +0000 | [diff] [blame] | 116 | case RADIO_TUNED: |
| 117 | val = 0; |
| 118 | if (read_bytes[0] & 0x80) /* ready */ |
| 119 | { |
| 120 | val = read_bytes[2] & 0x7F; /* IF counter */ |
| 121 | val = (abs(val - 0x36) < 2); /* close match */ |
| 122 | } |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 123 | break; |
| 124 | |
| 125 | case RADIO_STEREO: |
Jörg Hohensohn | 4adf929 | 2004-10-17 23:24:18 +0000 | [diff] [blame] | 126 | val = read_bytes[2] >> 7; |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 127 | break; |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 128 | } |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 129 | |
Jörg Hohensohn | 8f8fbac | 2004-10-15 21:41:46 +0000 | [diff] [blame] | 130 | return val; |
| 131 | } |
Michael Sevakis | a2ee6a6 | 2007-03-16 14:40:40 +0000 | [diff] [blame] | 132 | |
Michael Sevakis | 7d759f6 | 2007-07-14 11:20:31 +0000 | [diff] [blame] | 133 | void tea5767_dbg_info(struct tea5767_dbg_info *info) |
Michael Sevakis | a2ee6a6 | 2007-03-16 14:40:40 +0000 | [diff] [blame] | 134 | { |
| 135 | fmradio_i2c_read(I2C_ADR, info->read_regs, 5); |
| 136 | memcpy(info->write_regs, write_bytes, 5); |
| 137 | } |