xref: /openbmc/linux/drivers/media/radio/radio-timb.c (revision 22d55f02)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * radio-timb.c Timberdale FPGA Radio driver
4  * Copyright (c) 2009 Intel Corporation
5  */
6 
7 #include <linux/io.h>
8 #include <media/v4l2-ioctl.h>
9 #include <media/v4l2-device.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-event.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/module.h>
17 #include <linux/platform_data/media/timb_radio.h>
18 
19 #define DRIVER_NAME "timb-radio"
20 
21 struct timbradio {
22 	struct timb_radio_platform_data	pdata;
23 	struct v4l2_subdev	*sd_tuner;
24 	struct v4l2_subdev	*sd_dsp;
25 	struct video_device	video_dev;
26 	struct v4l2_device	v4l2_dev;
27 	struct mutex		lock;
28 };
29 
30 
31 static int timbradio_vidioc_querycap(struct file *file, void  *priv,
32 	struct v4l2_capability *v)
33 {
34 	strscpy(v->driver, DRIVER_NAME, sizeof(v->driver));
35 	strscpy(v->card, "Timberdale Radio", sizeof(v->card));
36 	snprintf(v->bus_info, sizeof(v->bus_info), "platform:"DRIVER_NAME);
37 	v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
38 	v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
39 	return 0;
40 }
41 
42 static int timbradio_vidioc_g_tuner(struct file *file, void *priv,
43 	struct v4l2_tuner *v)
44 {
45 	struct timbradio *tr = video_drvdata(file);
46 	return v4l2_subdev_call(tr->sd_tuner, tuner, g_tuner, v);
47 }
48 
49 static int timbradio_vidioc_s_tuner(struct file *file, void *priv,
50 	const struct v4l2_tuner *v)
51 {
52 	struct timbradio *tr = video_drvdata(file);
53 	return v4l2_subdev_call(tr->sd_tuner, tuner, s_tuner, v);
54 }
55 
56 static int timbradio_vidioc_s_frequency(struct file *file, void *priv,
57 	const struct v4l2_frequency *f)
58 {
59 	struct timbradio *tr = video_drvdata(file);
60 	return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f);
61 }
62 
63 static int timbradio_vidioc_g_frequency(struct file *file, void *priv,
64 	struct v4l2_frequency *f)
65 {
66 	struct timbradio *tr = video_drvdata(file);
67 	return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f);
68 }
69 
70 static const struct v4l2_ioctl_ops timbradio_ioctl_ops = {
71 	.vidioc_querycap	= timbradio_vidioc_querycap,
72 	.vidioc_g_tuner		= timbradio_vidioc_g_tuner,
73 	.vidioc_s_tuner		= timbradio_vidioc_s_tuner,
74 	.vidioc_g_frequency	= timbradio_vidioc_g_frequency,
75 	.vidioc_s_frequency	= timbradio_vidioc_s_frequency,
76 	.vidioc_log_status      = v4l2_ctrl_log_status,
77 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
78 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
79 };
80 
81 static const struct v4l2_file_operations timbradio_fops = {
82 	.owner		= THIS_MODULE,
83 	.open		= v4l2_fh_open,
84 	.release	= v4l2_fh_release,
85 	.poll		= v4l2_ctrl_poll,
86 	.unlocked_ioctl	= video_ioctl2,
87 };
88 
89 static int timbradio_probe(struct platform_device *pdev)
90 {
91 	struct timb_radio_platform_data *pdata = pdev->dev.platform_data;
92 	struct timbradio *tr;
93 	int err;
94 
95 	if (!pdata) {
96 		dev_err(&pdev->dev, "Platform data missing\n");
97 		err = -EINVAL;
98 		goto err;
99 	}
100 
101 	tr = devm_kzalloc(&pdev->dev, sizeof(*tr), GFP_KERNEL);
102 	if (!tr) {
103 		err = -ENOMEM;
104 		goto err;
105 	}
106 
107 	tr->pdata = *pdata;
108 	mutex_init(&tr->lock);
109 
110 	strscpy(tr->video_dev.name, "Timberdale Radio",
111 		sizeof(tr->video_dev.name));
112 	tr->video_dev.fops = &timbradio_fops;
113 	tr->video_dev.ioctl_ops = &timbradio_ioctl_ops;
114 	tr->video_dev.release = video_device_release_empty;
115 	tr->video_dev.minor = -1;
116 	tr->video_dev.lock = &tr->lock;
117 
118 	strscpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name));
119 	err = v4l2_device_register(NULL, &tr->v4l2_dev);
120 	if (err)
121 		goto err;
122 
123 	tr->video_dev.v4l2_dev = &tr->v4l2_dev;
124 
125 	tr->sd_tuner = v4l2_i2c_new_subdev_board(&tr->v4l2_dev,
126 		i2c_get_adapter(pdata->i2c_adapter), pdata->tuner, NULL);
127 	tr->sd_dsp = v4l2_i2c_new_subdev_board(&tr->v4l2_dev,
128 		i2c_get_adapter(pdata->i2c_adapter), pdata->dsp, NULL);
129 	if (tr->sd_tuner == NULL || tr->sd_dsp == NULL) {
130 		err = -ENODEV;
131 		goto err_video_req;
132 	}
133 
134 	tr->v4l2_dev.ctrl_handler = tr->sd_dsp->ctrl_handler;
135 
136 	err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1);
137 	if (err) {
138 		dev_err(&pdev->dev, "Error reg video\n");
139 		goto err_video_req;
140 	}
141 
142 	video_set_drvdata(&tr->video_dev, tr);
143 
144 	platform_set_drvdata(pdev, tr);
145 	return 0;
146 
147 err_video_req:
148 	v4l2_device_unregister(&tr->v4l2_dev);
149 err:
150 	dev_err(&pdev->dev, "Failed to register: %d\n", err);
151 
152 	return err;
153 }
154 
155 static int timbradio_remove(struct platform_device *pdev)
156 {
157 	struct timbradio *tr = platform_get_drvdata(pdev);
158 
159 	video_unregister_device(&tr->video_dev);
160 	v4l2_device_unregister(&tr->v4l2_dev);
161 	return 0;
162 }
163 
164 static struct platform_driver timbradio_platform_driver = {
165 	.driver = {
166 		.name	= DRIVER_NAME,
167 	},
168 	.probe		= timbradio_probe,
169 	.remove		= timbradio_remove,
170 };
171 
172 module_platform_driver(timbradio_platform_driver);
173 
174 MODULE_DESCRIPTION("Timberdale Radio driver");
175 MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
176 MODULE_LICENSE("GPL v2");
177 MODULE_VERSION("0.0.2");
178 MODULE_ALIAS("platform:"DRIVER_NAME);
179