2 * camera_common.c - utilities for tegra camera driver
4 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <media/camera_common.h>
20 #include <mach/io_dpd.h>
22 #define has_s_op(master, op) \
23 (master->ops && master->ops->op)
24 #define call_s_op(master, op) \
25 (has_s_op(master, op) ? \
26 master->ops->op(master) : 0)
27 #define call_s_ops(master, op, ...) \
28 (has_s_op(master, op) ? \
29 master->ops->op(master, __VA_ARGS__) : 0)
31 static const struct camera_common_colorfmt camera_common_color_fmts[] = {
32 {V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_COLORSPACE_SRGB},
33 {V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_COLORSPACE_SRGB},
34 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB},
35 {V4L2_MBUS_FMT_RGBA8888_4X8_LE, V4L2_COLORSPACE_SRGB},
38 static struct tegra_io_dpd camera_common_csi_io[] = {
41 .io_dpd_reg_index = 0,
46 .io_dpd_reg_index = 0,
51 .io_dpd_reg_index = 1,
56 .io_dpd_reg_index = 1,
61 .io_dpd_reg_index = 1,
66 .io_dpd_reg_index = 1,
71 int camera_common_g_ctrl(struct camera_common_data *s_data,
72 struct v4l2_control *control)
76 for (i = 0; i < s_data->numctrls; i++) {
77 if (s_data->ctrls[i]->id == control->id) {
78 control->value = s_data->ctrls[i]->val;
79 dev_dbg(&s_data->i2c_client->dev,
80 "%s: found control %s\n", __func__,
81 s_data->ctrls[i]->name);
88 EXPORT_SYMBOL(camera_common_g_ctrl);
90 int camera_common_regulator_get(struct i2c_client *client,
91 struct regulator **vreg, const char *vreg_name)
93 struct regulator *reg = NULL;
96 reg = devm_regulator_get(&client->dev, vreg_name);
97 if (unlikely(IS_ERR(reg))) {
98 dev_err(&client->dev, "%s %s ERR: %p\n",
99 __func__, vreg_name, reg);
103 dev_dbg(&client->dev, "%s: %s\n",
104 __func__, vreg_name);
109 EXPORT_SYMBOL(camera_common_regulator_get);
111 int camera_common_debugfs_show(struct seq_file *s, void *unused)
113 struct camera_common_data *s_data = s->private;
115 dev_dbg(&s_data->i2c_client->dev, "%s: ++\n", __func__);
120 ssize_t camera_common_debugfs_write(
122 char const __user *buf,
126 struct camera_common_data *s_data =
127 ((struct seq_file *)file->private_data)->private;
128 struct i2c_client *client = s_data->i2c_client;
130 char buffer[MAX_BUFFER_SIZE];
135 dev_dbg(&client->dev, "%s: ++\n", __func__);
137 if (copy_from_user(&buffer, buf, sizeof(buffer)))
138 goto debugfs_write_fail;
140 if (sscanf(buf, "0x%x 0x%x", &address, &data) == 2)
142 if (sscanf(buf, "0X%x 0X%x", &address, &data) == 2)
144 if (sscanf(buf, "%d %d", &address, &data) == 2)
147 if (sscanf(buf, "0x%x 0x%x", &address, &data) == 1)
149 if (sscanf(buf, "0X%x 0X%x", &address, &data) == 1)
151 if (sscanf(buf, "%d %d", &address, &data) == 1)
154 dev_err(&client->dev, "SYNTAX ERROR: %s\n", buf);
158 dev_info(&client->dev,
159 "new address = %x, data = %x\n", address, data);
160 err |= call_s_ops(s_data, write_reg, address, data);
162 err |= call_s_ops(s_data, read_reg, address, &readback);
163 dev_info(&client->dev,
164 "wrote to address 0x%x with value 0x%x\n",
168 goto debugfs_write_fail;
173 dev_err(&client->dev,
174 "%s: test pattern write failed\n", __func__);
178 int camera_common_debugfs_open(struct inode *inode, struct file *file)
180 struct camera_common_data *s_data = inode->i_private;
181 struct i2c_client *client = s_data->i2c_client;
183 dev_dbg(&client->dev, "%s: ++\n", __func__);
185 return single_open(file, camera_common_debugfs_show, inode->i_private);
188 static const struct file_operations camera_common_debugfs_fops = {
189 .open = camera_common_debugfs_open,
191 .write = camera_common_debugfs_write,
193 .release = single_release,
196 void camera_common_remove_debugfs(
197 struct camera_common_data *s_data)
199 struct i2c_client *client = s_data->i2c_client;
201 dev_dbg(&client->dev, "%s: ++\n", __func__);
203 debugfs_remove_recursive(s_data->debugdir);
204 s_data->debugdir = NULL;
206 EXPORT_SYMBOL(camera_common_remove_debugfs);
208 void camera_common_create_debugfs(
209 struct camera_common_data *s_data,
213 struct i2c_client *client = s_data->i2c_client;
215 dev_dbg(&client->dev, "%s %s\n", __func__, name);
218 debugfs_create_dir(name, NULL);
219 if (!s_data->debugdir)
222 err = debugfs_create_file("d",
224 s_data->debugdir, s_data,
225 &camera_common_debugfs_fops);
231 dev_err(&client->dev, "couldn't create debugfs\n");
232 camera_common_remove_debugfs(s_data);
234 EXPORT_SYMBOL(camera_common_create_debugfs);
236 /* Find a data format by a pixel code in an array */
237 const struct camera_common_colorfmt *camera_common_find_datafmt(
238 enum v4l2_mbus_pixelcode code)
242 for (i = 0; i < ARRAY_SIZE(camera_common_color_fmts); i++)
243 if (camera_common_color_fmts[i].code == code)
244 return camera_common_color_fmts + i;
248 EXPORT_SYMBOL(camera_common_find_datafmt);
250 int camera_common_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
251 enum v4l2_mbus_pixelcode *code)
253 if ((unsigned int)index >= ARRAY_SIZE(camera_common_color_fmts))
256 *code = camera_common_color_fmts[index].code;
259 EXPORT_SYMBOL(camera_common_enum_fmt);
261 int camera_common_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
263 struct i2c_client *client = v4l2_get_subdevdata(sd);
264 struct camera_common_data *s_data = to_camera_common_data(client);
265 struct v4l2_control hdr_control;
266 const struct camera_common_frmfmt *frmfmt = s_data->frmfmt;
270 bool fmt_match = false;
272 dev_dbg(&client->dev, "%s: size %i x %i\n", __func__,
273 mf->width, mf->height);
275 /* check hdr enable ctrl */
276 hdr_control.id = V4L2_CID_HDR_EN;
278 err = v4l2_g_ctrl(s_data->ctrl_handler, &hdr_control);
280 dev_dbg(&client->dev, "could not find device ctrl.\n");
281 hdr_control.value = 0;
284 hdr_en = switch_ctrl_qmenu[hdr_control.value];
286 s_data->mode = s_data->def_mode;
287 s_data->fmt_width = s_data->def_width;
288 s_data->fmt_height = s_data->def_height;
290 for (i = 0; i < s_data->numfmts; i++) {
291 if (mf->width == frmfmt[i].size.width &&
292 mf->height == frmfmt[i].size.height &&
293 hdr_en == frmfmt[i].hdr_en) {
294 s_data->mode = frmfmt[i].mode;
295 s_data->fmt_width = mf->width;
296 s_data->fmt_height = mf->height;
302 if (i == s_data->numfmts)
303 dev_dbg(&client->dev,
304 "%s: invalid resolution supplied to set mode %d %d\n",
305 __func__, mf->width, mf->height);
307 if (mf->code != V4L2_MBUS_FMT_SRGGB8_1X8 &&
308 mf->code != V4L2_MBUS_FMT_SRGGB10_1X10 &&
309 mf->code != V4L2_MBUS_FMT_RGBA8888_4X8_LE &&
310 mf->code != V4L2_MBUS_FMT_UYVY8_2X8)
311 mf->code = V4L2_MBUS_FMT_SRGGB10_1X10;
314 mf->width = s_data->def_width;
315 mf->height = s_data->def_height;
318 mf->field = V4L2_FIELD_NONE;
319 mf->colorspace = V4L2_COLORSPACE_SRGB;
323 EXPORT_SYMBOL(camera_common_try_fmt);
325 int camera_common_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
327 struct i2c_client *client = v4l2_get_subdevdata(sd);
328 struct camera_common_data *s_data = to_camera_common_data(client);
330 dev_dbg(&client->dev, "%s(%u) size %i x %i\n", __func__,
331 mf->code, mf->width, mf->height);
333 /* MIPI CSI could have changed the format, double-check */
334 if (!camera_common_find_datafmt(mf->code))
337 camera_common_try_fmt(sd, mf);
339 s_data->colorfmt = camera_common_find_datafmt(mf->code);
343 EXPORT_SYMBOL(camera_common_s_fmt);
345 int camera_common_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
347 struct i2c_client *client = v4l2_get_subdevdata(sd);
348 struct camera_common_data *s_data = to_camera_common_data(client);
349 const struct camera_common_colorfmt *fmt = s_data->colorfmt;
351 dev_dbg(&client->dev, "%s++\n", __func__);
353 mf->code = fmt->code;
354 mf->colorspace = fmt->colorspace;
355 mf->width = s_data->fmt_width;
356 mf->height = s_data->fmt_height;
357 mf->field = V4L2_FIELD_NONE;
361 EXPORT_SYMBOL(camera_common_g_fmt);
363 int camera_common_g_chip_ident(struct v4l2_subdev *sd,
364 struct v4l2_dbg_chip_ident *id)
366 struct i2c_client *client = v4l2_get_subdevdata(sd);
367 struct camera_common_data *s_data = to_camera_common_data(client);
369 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
372 if (id->match.addr != client->addr)
375 id->ident = s_data->ident;
381 static void camera_common_mclk_disable(struct camera_common_data *s_data)
383 struct camera_common_power_rail *pw = s_data->power;
386 dev_err(&s_data->i2c_client->dev, "%s: no device power rail\n",
391 dev_dbg(&s_data->i2c_client->dev, "%s: disable MCLK\n", __func__);
392 clk_disable_unprepare(pw->mclk);
395 static int camera_common_mclk_enable(struct camera_common_data *s_data)
398 struct camera_common_power_rail *pw = s_data->power;
399 unsigned long mclk_init_rate = s_data->def_clk_freq;
402 dev_err(&s_data->i2c_client->dev, "%s: no device power rail\n",
407 dev_dbg(&s_data->i2c_client->dev, "%s: enable MCLK with %lu Hz\n",
408 __func__, mclk_init_rate);
410 err = clk_set_rate(pw->mclk, mclk_init_rate);
412 err = clk_prepare_enable(pw->mclk);
416 static void camera_common_dpd_disable(struct camera_common_data *s_data)
420 /* 2 lanes per port, divide by two to get numports */
421 int numports = (s_data->numlanes + 1) >> 1;
423 /* disable CSI IOs DPD mode to turn on camera */
424 for (i = 0; i < numports; i++) {
425 io_idx = s_data->csi_port + i;
426 tegra_io_dpd_disable(&camera_common_csi_io[io_idx]);
427 dev_dbg(&s_data->i2c_client->dev,
428 "%s: csi %d\n", __func__, io_idx);
432 static void camera_common_dpd_enable(struct camera_common_data *s_data)
436 /* 2 lanes per port, divide by two to get numports */
437 int numports = (s_data->numlanes + 1) >> 1;
439 /* disable CSI IOs DPD mode to turn on camera */
440 for (i = 0; i < numports; i++) {
441 io_idx = s_data->csi_port + i;
442 tegra_io_dpd_enable(&camera_common_csi_io[io_idx]);
443 dev_dbg(&s_data->i2c_client->dev,
444 "%s: csi %d\n", __func__, io_idx);
448 int camera_common_s_power(struct v4l2_subdev *sd, int on)
451 struct i2c_client *client = v4l2_get_subdevdata(sd);
452 struct camera_common_data *s_data = to_camera_common_data(client);
455 err = camera_common_mclk_enable(s_data);
456 camera_common_dpd_disable(s_data);
458 err = call_s_op(s_data, power_on);
460 camera_common_dpd_enable(s_data);
461 camera_common_mclk_disable(s_data);
465 call_s_op(s_data, power_off);
466 camera_common_dpd_enable(s_data);
467 camera_common_mclk_disable(s_data);
472 EXPORT_SYMBOL(camera_common_s_power);
474 int camera_common_g_mbus_config(struct v4l2_subdev *sd,
475 struct v4l2_mbus_config *cfg)
477 cfg->type = V4L2_MBUS_CSI2;
478 cfg->flags = V4L2_MBUS_CSI2_4_LANE |
479 V4L2_MBUS_CSI2_CHANNEL_0 |
480 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
484 EXPORT_SYMBOL(camera_common_g_mbus_config);