Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * tef6862.c Philips TEF6862 Car Radio Enhanced Selectivity Tuner
0004  * Copyright (c) 2009 Intel Corporation
0005  */
0006 
0007 #include <linux/module.h>
0008 #include <linux/init.h>
0009 #include <linux/errno.h>
0010 #include <linux/kernel.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/i2c.h>
0013 #include <linux/slab.h>
0014 #include <media/v4l2-ioctl.h>
0015 #include <media/v4l2-device.h>
0016 
0017 #define DRIVER_NAME "tef6862"
0018 
0019 #define FREQ_MUL 16000
0020 
0021 #define TEF6862_LO_FREQ (875U * FREQ_MUL / 10)
0022 #define TEF6862_HI_FREQ (108U * FREQ_MUL)
0023 
0024 /* Write mode sub addresses */
0025 #define WM_SUB_BANDWIDTH    0x0
0026 #define WM_SUB_PLLM     0x1
0027 #define WM_SUB_PLLL     0x2
0028 #define WM_SUB_DAA      0x3
0029 #define WM_SUB_AGC      0x4
0030 #define WM_SUB_BAND     0x5
0031 #define WM_SUB_CONTROL      0x6
0032 #define WM_SUB_LEVEL        0x7
0033 #define WM_SUB_IFCF     0x8
0034 #define WM_SUB_IFCAP        0x9
0035 #define WM_SUB_ACD      0xA
0036 #define WM_SUB_TEST     0xF
0037 
0038 /* Different modes of the MSA register */
0039 #define MSA_MODE_BUFFER     0x0
0040 #define MSA_MODE_PRESET     0x1
0041 #define MSA_MODE_SEARCH     0x2
0042 #define MSA_MODE_AF_UPDATE  0x3
0043 #define MSA_MODE_JUMP       0x4
0044 #define MSA_MODE_CHECK      0x5
0045 #define MSA_MODE_LOAD       0x6
0046 #define MSA_MODE_END        0x7
0047 #define MSA_MODE_SHIFT      5
0048 
0049 struct tef6862_state {
0050     struct v4l2_subdev sd;
0051     unsigned long freq;
0052 };
0053 
0054 static inline struct tef6862_state *to_state(struct v4l2_subdev *sd)
0055 {
0056     return container_of(sd, struct tef6862_state, sd);
0057 }
0058 
0059 static u16 tef6862_sigstr(struct i2c_client *client)
0060 {
0061     u8 buf[4];
0062     int err = i2c_master_recv(client, buf, sizeof(buf));
0063     if (err == sizeof(buf))
0064         return buf[3] << 8;
0065     return 0;
0066 }
0067 
0068 static int tef6862_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
0069 {
0070     if (v->index > 0)
0071         return -EINVAL;
0072 
0073     /* only support FM for now */
0074     strscpy(v->name, "FM", sizeof(v->name));
0075     v->type = V4L2_TUNER_RADIO;
0076     v->rangelow = TEF6862_LO_FREQ;
0077     v->rangehigh = TEF6862_HI_FREQ;
0078     v->rxsubchans = V4L2_TUNER_SUB_MONO;
0079     v->capability = V4L2_TUNER_CAP_LOW;
0080     v->audmode = V4L2_TUNER_MODE_STEREO;
0081     v->signal = tef6862_sigstr(v4l2_get_subdevdata(sd));
0082 
0083     return 0;
0084 }
0085 
0086 static int tef6862_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
0087 {
0088     return v->index ? -EINVAL : 0;
0089 }
0090 
0091 static int tef6862_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
0092 {
0093     struct tef6862_state *state = to_state(sd);
0094     struct i2c_client *client = v4l2_get_subdevdata(sd);
0095     unsigned freq = f->frequency;
0096     u16 pll;
0097     u8 i2cmsg[3];
0098     int err;
0099 
0100     if (f->tuner != 0)
0101         return -EINVAL;
0102 
0103     freq = clamp(freq, TEF6862_LO_FREQ, TEF6862_HI_FREQ);
0104     pll = 1964 + ((freq - TEF6862_LO_FREQ) * 20) / FREQ_MUL;
0105     i2cmsg[0] = (MSA_MODE_PRESET << MSA_MODE_SHIFT) | WM_SUB_PLLM;
0106     i2cmsg[1] = (pll >> 8) & 0xff;
0107     i2cmsg[2] = pll & 0xff;
0108 
0109     err = i2c_master_send(client, i2cmsg, sizeof(i2cmsg));
0110     if (err != sizeof(i2cmsg))
0111         return err < 0 ? err : -EIO;
0112 
0113     state->freq = freq;
0114     return 0;
0115 }
0116 
0117 static int tef6862_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
0118 {
0119     struct tef6862_state *state = to_state(sd);
0120 
0121     if (f->tuner != 0)
0122         return -EINVAL;
0123     f->type = V4L2_TUNER_RADIO;
0124     f->frequency = state->freq;
0125     return 0;
0126 }
0127 
0128 static const struct v4l2_subdev_tuner_ops tef6862_tuner_ops = {
0129     .g_tuner = tef6862_g_tuner,
0130     .s_tuner = tef6862_s_tuner,
0131     .s_frequency = tef6862_s_frequency,
0132     .g_frequency = tef6862_g_frequency,
0133 };
0134 
0135 static const struct v4l2_subdev_ops tef6862_ops = {
0136     .tuner = &tef6862_tuner_ops,
0137 };
0138 
0139 /*
0140  * Generic i2c probe
0141  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
0142  */
0143 
0144 static int tef6862_probe(struct i2c_client *client,
0145              const struct i2c_device_id *id)
0146 {
0147     struct tef6862_state *state;
0148     struct v4l2_subdev *sd;
0149 
0150     /* Check if the adapter supports the needed features */
0151     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0152         return -EIO;
0153 
0154     v4l_info(client, "chip found @ 0x%02x (%s)\n",
0155             client->addr << 1, client->adapter->name);
0156 
0157     state = kzalloc(sizeof(struct tef6862_state), GFP_KERNEL);
0158     if (state == NULL)
0159         return -ENOMEM;
0160     state->freq = TEF6862_LO_FREQ;
0161 
0162     sd = &state->sd;
0163     v4l2_i2c_subdev_init(sd, client, &tef6862_ops);
0164 
0165     return 0;
0166 }
0167 
0168 static int tef6862_remove(struct i2c_client *client)
0169 {
0170     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0171 
0172     v4l2_device_unregister_subdev(sd);
0173     kfree(to_state(sd));
0174     return 0;
0175 }
0176 
0177 static const struct i2c_device_id tef6862_id[] = {
0178     {DRIVER_NAME, 0},
0179     {},
0180 };
0181 
0182 MODULE_DEVICE_TABLE(i2c, tef6862_id);
0183 
0184 static struct i2c_driver tef6862_driver = {
0185     .driver = {
0186         .name   = DRIVER_NAME,
0187     },
0188     .probe      = tef6862_probe,
0189     .remove     = tef6862_remove,
0190     .id_table   = tef6862_id,
0191 };
0192 
0193 module_i2c_driver(tef6862_driver);
0194 
0195 MODULE_DESCRIPTION("TEF6862 Car Radio Enhanced Selectivity Tuner");
0196 MODULE_AUTHOR("Mocean Laboratories");
0197 MODULE_LICENSE("GPL v2");