Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * vp27smpx - driver version 0.0.1
0004  *
0005  * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
0006  *
0007  * Based on a tvaudio patch from Takahiro Adachi <tadachi@tadachi-net.com>
0008  * and Kazuhiko Kawakami <kazz-0@mail.goo.ne.jp>
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/types.h>
0013 #include <linux/slab.h>
0014 #include <linux/ioctl.h>
0015 #include <linux/uaccess.h>
0016 #include <linux/i2c.h>
0017 #include <linux/videodev2.h>
0018 #include <media/v4l2-device.h>
0019 
0020 MODULE_DESCRIPTION("vp27smpx driver");
0021 MODULE_AUTHOR("Hans Verkuil");
0022 MODULE_LICENSE("GPL");
0023 
0024 
0025 /* ----------------------------------------------------------------------- */
0026 
0027 struct vp27smpx_state {
0028     struct v4l2_subdev sd;
0029     int radio;
0030     u32 audmode;
0031 };
0032 
0033 static inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd)
0034 {
0035     return container_of(sd, struct vp27smpx_state, sd);
0036 }
0037 
0038 static void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode)
0039 {
0040     struct vp27smpx_state *state = to_state(sd);
0041     struct i2c_client *client = v4l2_get_subdevdata(sd);
0042     u8 data[3] = { 0x00, 0x00, 0x04 };
0043 
0044     switch (audmode) {
0045     case V4L2_TUNER_MODE_MONO:
0046     case V4L2_TUNER_MODE_LANG1:
0047         break;
0048     case V4L2_TUNER_MODE_STEREO:
0049     case V4L2_TUNER_MODE_LANG1_LANG2:
0050         data[1] = 0x01;
0051         break;
0052     case V4L2_TUNER_MODE_LANG2:
0053         data[1] = 0x02;
0054         break;
0055     }
0056 
0057     if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
0058         v4l2_err(sd, "I/O error setting audmode\n");
0059     else
0060         state->audmode = audmode;
0061 }
0062 
0063 static int vp27smpx_s_radio(struct v4l2_subdev *sd)
0064 {
0065     struct vp27smpx_state *state = to_state(sd);
0066 
0067     state->radio = 1;
0068     return 0;
0069 }
0070 
0071 static int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
0072 {
0073     struct vp27smpx_state *state = to_state(sd);
0074 
0075     state->radio = 0;
0076     return 0;
0077 }
0078 
0079 static int vp27smpx_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
0080 {
0081     struct vp27smpx_state *state = to_state(sd);
0082 
0083     if (!state->radio)
0084         vp27smpx_set_audmode(sd, vt->audmode);
0085     return 0;
0086 }
0087 
0088 static int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
0089 {
0090     struct vp27smpx_state *state = to_state(sd);
0091 
0092     if (state->radio)
0093         return 0;
0094     vt->audmode = state->audmode;
0095     vt->capability = V4L2_TUNER_CAP_STEREO |
0096         V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
0097     vt->rxsubchans = V4L2_TUNER_SUB_MONO;
0098     return 0;
0099 }
0100 
0101 static int vp27smpx_log_status(struct v4l2_subdev *sd)
0102 {
0103     struct vp27smpx_state *state = to_state(sd);
0104 
0105     v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode,
0106             state->radio ? " (Radio)" : "");
0107     return 0;
0108 }
0109 
0110 /* ----------------------------------------------------------------------- */
0111 
0112 static const struct v4l2_subdev_core_ops vp27smpx_core_ops = {
0113     .log_status = vp27smpx_log_status,
0114 };
0115 
0116 static const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = {
0117     .s_radio = vp27smpx_s_radio,
0118     .s_tuner = vp27smpx_s_tuner,
0119     .g_tuner = vp27smpx_g_tuner,
0120 };
0121 
0122 static const struct v4l2_subdev_video_ops vp27smpx_video_ops = {
0123     .s_std = vp27smpx_s_std,
0124 };
0125 
0126 static const struct v4l2_subdev_ops vp27smpx_ops = {
0127     .core = &vp27smpx_core_ops,
0128     .tuner = &vp27smpx_tuner_ops,
0129     .video = &vp27smpx_video_ops,
0130 };
0131 
0132 /* ----------------------------------------------------------------------- */
0133 
0134 /* i2c implementation */
0135 
0136 /*
0137  * Generic i2c probe
0138  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
0139  */
0140 
0141 static int vp27smpx_probe(struct i2c_client *client,
0142               const struct i2c_device_id *id)
0143 {
0144     struct vp27smpx_state *state;
0145     struct v4l2_subdev *sd;
0146 
0147     /* Check if the adapter supports the needed features */
0148     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0149         return -EIO;
0150 
0151     v4l_info(client, "chip found @ 0x%x (%s)\n",
0152             client->addr << 1, client->adapter->name);
0153 
0154     state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
0155     if (state == NULL)
0156         return -ENOMEM;
0157     sd = &state->sd;
0158     v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops);
0159     state->audmode = V4L2_TUNER_MODE_STEREO;
0160 
0161     /* initialize vp27smpx */
0162     vp27smpx_set_audmode(sd, state->audmode);
0163     return 0;
0164 }
0165 
0166 static int vp27smpx_remove(struct i2c_client *client)
0167 {
0168     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0169 
0170     v4l2_device_unregister_subdev(sd);
0171     return 0;
0172 }
0173 
0174 /* ----------------------------------------------------------------------- */
0175 
0176 static const struct i2c_device_id vp27smpx_id[] = {
0177     { "vp27smpx", 0 },
0178     { }
0179 };
0180 MODULE_DEVICE_TABLE(i2c, vp27smpx_id);
0181 
0182 static struct i2c_driver vp27smpx_driver = {
0183     .driver = {
0184         .name   = "vp27smpx",
0185     },
0186     .probe      = vp27smpx_probe,
0187     .remove     = vp27smpx_remove,
0188     .id_table   = vp27smpx_id,
0189 };
0190 
0191 module_i2c_driver(vp27smpx_driver);