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