aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans Verkuil <hans.verkuil@cisco.com>2013-05-31 05:17:42 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2013-06-17 08:24:42 -0400
commit04de560296158c481570a020748d1239e493617b (patch)
tree8039ba879e0d6d21d392636734fd756dd0fa4c4a
parent8ff618320ce304cf96d84dcacd606d3b7f82c78d (diff)
[media] sr030pc30: convert to the control framework
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Acked-by: Sylwester Nawrocki <s.nawrocki@samsung.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
-rw-r--r--drivers/media/i2c/sr030pc30.c276
1 files changed, 88 insertions, 188 deletions
diff --git a/drivers/media/i2c/sr030pc30.c b/drivers/media/i2c/sr030pc30.c
index 4c5a9ee60c3e..ae9432637fcb 100644
--- a/drivers/media/i2c/sr030pc30.c
+++ b/drivers/media/i2c/sr030pc30.c
@@ -23,6 +23,7 @@
23#include <media/v4l2-device.h> 23#include <media/v4l2-device.h>
24#include <media/v4l2-subdev.h> 24#include <media/v4l2-subdev.h>
25#include <media/v4l2-mediabus.h> 25#include <media/v4l2-mediabus.h>
26#include <media/v4l2-ctrls.h>
26#include <media/sr030pc30.h> 27#include <media/sr030pc30.h>
27 28
28static int debug; 29static int debug;
@@ -142,17 +143,24 @@ module_param(debug, int, 0644);
142 143
143struct sr030pc30_info { 144struct sr030pc30_info {
144 struct v4l2_subdev sd; 145 struct v4l2_subdev sd;
146 struct v4l2_ctrl_handler hdl;
145 const struct sr030pc30_platform_data *pdata; 147 const struct sr030pc30_platform_data *pdata;
146 const struct sr030pc30_format *curr_fmt; 148 const struct sr030pc30_format *curr_fmt;
147 const struct sr030pc30_frmsize *curr_win; 149 const struct sr030pc30_frmsize *curr_win;
148 unsigned int auto_wb:1;
149 unsigned int auto_exp:1;
150 unsigned int hflip:1; 150 unsigned int hflip:1;
151 unsigned int vflip:1; 151 unsigned int vflip:1;
152 unsigned int sleep:1; 152 unsigned int sleep:1;
153 unsigned int exposure; 153 struct {
154 u8 blue_balance; 154 /* auto whitebalance control cluster */
155 u8 red_balance; 155 struct v4l2_ctrl *awb;
156 struct v4l2_ctrl *red;
157 struct v4l2_ctrl *blue;
158 };
159 struct {
160 /* auto exposure control cluster */
161 struct v4l2_ctrl *autoexp;
162 struct v4l2_ctrl *exp;
163 };
156 u8 i2c_reg_page; 164 u8 i2c_reg_page;
157}; 165};
158 166
@@ -173,52 +181,6 @@ struct i2c_regval {
173 u16 val; 181 u16 val;
174}; 182};
175 183
176static const struct v4l2_queryctrl sr030pc30_ctrl[] = {
177 {
178 .id = V4L2_CID_AUTO_WHITE_BALANCE,
179 .type = V4L2_CTRL_TYPE_BOOLEAN,
180 .name = "Auto White Balance",
181 .minimum = 0,
182 .maximum = 1,
183 .step = 1,
184 .default_value = 1,
185 }, {
186 .id = V4L2_CID_RED_BALANCE,
187 .type = V4L2_CTRL_TYPE_INTEGER,
188 .name = "Red Balance",
189 .minimum = 0,
190 .maximum = 127,
191 .step = 1,
192 .default_value = 64,
193 .flags = 0,
194 }, {
195 .id = V4L2_CID_BLUE_BALANCE,
196 .type = V4L2_CTRL_TYPE_INTEGER,
197 .name = "Blue Balance",
198 .minimum = 0,
199 .maximum = 127,
200 .step = 1,
201 .default_value = 64,
202 }, {
203 .id = V4L2_CID_EXPOSURE_AUTO,
204 .type = V4L2_CTRL_TYPE_INTEGER,
205 .name = "Auto Exposure",
206 .minimum = 0,
207 .maximum = 1,
208 .step = 1,
209 .default_value = 1,
210 }, {
211 .id = V4L2_CID_EXPOSURE,
212 .type = V4L2_CTRL_TYPE_INTEGER,
213 .name = "Exposure",
214 .minimum = EXPOS_MIN_MS,
215 .maximum = EXPOS_MAX_MS,
216 .step = 1,
217 .default_value = 1,
218 }, {
219 }
220};
221
222/* supported resolutions */ 184/* supported resolutions */
223static const struct sr030pc30_frmsize sr030pc30_sizes[] = { 185static const struct sr030pc30_frmsize sr030pc30_sizes[] = {
224 { 186 {
@@ -394,48 +356,6 @@ static int sr030pc30_pwr_ctrl(struct v4l2_subdev *sd,
394 return ret; 356 return ret;
395} 357}
396 358
397static inline int sr030pc30_enable_autoexposure(struct v4l2_subdev *sd, int on)
398{
399 struct sr030pc30_info *info = to_sr030pc30(sd);
400 /* auto anti-flicker is also enabled here */
401 int ret = cam_i2c_write(sd, AE_CTL1_REG, on ? 0xDC : 0x0C);
402 if (!ret)
403 info->auto_exp = on;
404 return ret;
405}
406
407static int sr030pc30_set_exposure(struct v4l2_subdev *sd, int value)
408{
409 struct sr030pc30_info *info = to_sr030pc30(sd);
410
411 unsigned long expos = value * info->pdata->clk_rate / (8 * 1000);
412
413 int ret = cam_i2c_write(sd, EXP_TIMEH_REG, expos >> 16 & 0xFF);
414 if (!ret)
415 ret = cam_i2c_write(sd, EXP_TIMEM_REG, expos >> 8 & 0xFF);
416 if (!ret)
417 ret = cam_i2c_write(sd, EXP_TIMEL_REG, expos & 0xFF);
418 if (!ret) { /* Turn off AE */
419 info->exposure = value;
420 ret = sr030pc30_enable_autoexposure(sd, 0);
421 }
422 return ret;
423}
424
425/* Automatic white balance control */
426static int sr030pc30_enable_autowhitebalance(struct v4l2_subdev *sd, int on)
427{
428 struct sr030pc30_info *info = to_sr030pc30(sd);
429
430 int ret = cam_i2c_write(sd, AWB_CTL2_REG, on ? 0x2E : 0x2F);
431 if (!ret)
432 ret = cam_i2c_write(sd, AWB_CTL1_REG, on ? 0xFB : 0x7B);
433 if (!ret)
434 info->auto_wb = on;
435
436 return ret;
437}
438
439static int sr030pc30_set_flip(struct v4l2_subdev *sd) 359static int sr030pc30_set_flip(struct v4l2_subdev *sd)
440{ 360{
441 struct sr030pc30_info *info = to_sr030pc30(sd); 361 struct sr030pc30_info *info = to_sr030pc30(sd);
@@ -498,107 +418,56 @@ static int sr030pc30_try_frame_size(struct v4l2_mbus_framefmt *mf)
498 return -EINVAL; 418 return -EINVAL;
499} 419}
500 420
501static int sr030pc30_queryctrl(struct v4l2_subdev *sd, 421static int sr030pc30_s_ctrl(struct v4l2_ctrl *ctrl)
502 struct v4l2_queryctrl *qc)
503{
504 int i;
505
506 for (i = 0; i < ARRAY_SIZE(sr030pc30_ctrl); i++)
507 if (qc->id == sr030pc30_ctrl[i].id) {
508 *qc = sr030pc30_ctrl[i];
509 v4l2_dbg(1, debug, sd, "%s id: %d\n",
510 __func__, qc->id);
511 return 0;
512 }
513
514 return -EINVAL;
515}
516
517static inline int sr030pc30_set_bluebalance(struct v4l2_subdev *sd, int value)
518{
519 int ret = cam_i2c_write(sd, MWB_BGAIN_REG, value);
520 if (!ret)
521 to_sr030pc30(sd)->blue_balance = value;
522 return ret;
523}
524
525static inline int sr030pc30_set_redbalance(struct v4l2_subdev *sd, int value)
526{
527 int ret = cam_i2c_write(sd, MWB_RGAIN_REG, value);
528 if (!ret)
529 to_sr030pc30(sd)->red_balance = value;
530 return ret;
531}
532
533static int sr030pc30_s_ctrl(struct v4l2_subdev *sd,
534 struct v4l2_control *ctrl)
535{ 422{
536 int i, ret = 0; 423 struct sr030pc30_info *info =
537 424 container_of(ctrl->handler, struct sr030pc30_info, hdl);
538 for (i = 0; i < ARRAY_SIZE(sr030pc30_ctrl); i++) 425 struct v4l2_subdev *sd = &info->sd;
539 if (ctrl->id == sr030pc30_ctrl[i].id) 426 int ret = 0;
540 break;
541
542 if (i == ARRAY_SIZE(sr030pc30_ctrl))
543 return -EINVAL;
544
545 if (ctrl->value < sr030pc30_ctrl[i].minimum ||
546 ctrl->value > sr030pc30_ctrl[i].maximum)
547 return -ERANGE;
548 427
549 v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n", 428 v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n",
550 __func__, ctrl->id, ctrl->value); 429 __func__, ctrl->id, ctrl->val);
551 430
552 switch (ctrl->id) { 431 switch (ctrl->id) {
553 case V4L2_CID_AUTO_WHITE_BALANCE: 432 case V4L2_CID_AUTO_WHITE_BALANCE:
554 sr030pc30_enable_autowhitebalance(sd, ctrl->value); 433 if (ctrl->is_new) {
555 break; 434 ret = cam_i2c_write(sd, AWB_CTL2_REG,
556 case V4L2_CID_BLUE_BALANCE: 435 ctrl->val ? 0x2E : 0x2F);
557 ret = sr030pc30_set_bluebalance(sd, ctrl->value); 436 if (!ret)
558 break; 437 ret = cam_i2c_write(sd, AWB_CTL1_REG,
559 case V4L2_CID_RED_BALANCE: 438 ctrl->val ? 0xFB : 0x7B);
560 ret = sr030pc30_set_redbalance(sd, ctrl->value); 439 }
561 break; 440 if (!ret && info->blue->is_new)
562 case V4L2_CID_EXPOSURE_AUTO: 441 ret = cam_i2c_write(sd, MWB_BGAIN_REG, info->blue->val);
563 sr030pc30_enable_autoexposure(sd, 442 if (!ret && info->red->is_new)
564 ctrl->value == V4L2_EXPOSURE_AUTO); 443 ret = cam_i2c_write(sd, MWB_RGAIN_REG, info->red->val);
565 break; 444 return ret;
566 case V4L2_CID_EXPOSURE:
567 ret = sr030pc30_set_exposure(sd, ctrl->value);
568 break;
569 default:
570 return -EINVAL;
571 }
572
573 return ret;
574}
575
576static int sr030pc30_g_ctrl(struct v4l2_subdev *sd,
577 struct v4l2_control *ctrl)
578{
579 struct sr030pc30_info *info = to_sr030pc30(sd);
580
581 v4l2_dbg(1, debug, sd, "%s: id: %d\n", __func__, ctrl->id);
582 445
583 switch (ctrl->id) {
584 case V4L2_CID_AUTO_WHITE_BALANCE:
585 ctrl->value = info->auto_wb;
586 break;
587 case V4L2_CID_BLUE_BALANCE:
588 ctrl->value = info->blue_balance;
589 break;
590 case V4L2_CID_RED_BALANCE:
591 ctrl->value = info->red_balance;
592 break;
593 case V4L2_CID_EXPOSURE_AUTO: 446 case V4L2_CID_EXPOSURE_AUTO:
594 ctrl->value = info->auto_exp; 447 /* auto anti-flicker is also enabled here */
595 break; 448 if (ctrl->is_new)
596 case V4L2_CID_EXPOSURE: 449 ret = cam_i2c_write(sd, AE_CTL1_REG,
597 ctrl->value = info->exposure; 450 ctrl->val == V4L2_EXPOSURE_AUTO ? 0xDC : 0x0C);
598 break; 451 if (info->exp->is_new) {
452 unsigned long expos = info->exp->val;
453
454 expos = expos * info->pdata->clk_rate / (8 * 1000);
455
456 if (!ret)
457 ret = cam_i2c_write(sd, EXP_TIMEH_REG,
458 expos >> 16 & 0xFF);
459 if (!ret)
460 ret = cam_i2c_write(sd, EXP_TIMEM_REG,
461 expos >> 8 & 0xFF);
462 if (!ret)
463 ret = cam_i2c_write(sd, EXP_TIMEL_REG,
464 expos & 0xFF);
465 }
466 return ret;
599 default: 467 default:
600 return -EINVAL; 468 return -EINVAL;
601 } 469 }
470
602 return 0; 471 return 0;
603} 472}
604 473
@@ -752,11 +621,19 @@ static int sr030pc30_s_power(struct v4l2_subdev *sd, int on)
752 return ret; 621 return ret;
753} 622}
754 623
624static const struct v4l2_ctrl_ops sr030pc30_ctrl_ops = {
625 .s_ctrl = sr030pc30_s_ctrl,
626};
627
755static const struct v4l2_subdev_core_ops sr030pc30_core_ops = { 628static const struct v4l2_subdev_core_ops sr030pc30_core_ops = {
756 .s_power = sr030pc30_s_power, 629 .s_power = sr030pc30_s_power,
757 .queryctrl = sr030pc30_queryctrl, 630 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
758 .s_ctrl = sr030pc30_s_ctrl, 631 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
759 .g_ctrl = sr030pc30_g_ctrl, 632 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
633 .g_ctrl = v4l2_subdev_g_ctrl,
634 .s_ctrl = v4l2_subdev_s_ctrl,
635 .queryctrl = v4l2_subdev_queryctrl,
636 .querymenu = v4l2_subdev_querymenu,
760}; 637};
761 638
762static const struct v4l2_subdev_video_ops sr030pc30_video_ops = { 639static const struct v4l2_subdev_video_ops sr030pc30_video_ops = {
@@ -807,6 +684,7 @@ static int sr030pc30_probe(struct i2c_client *client,
807{ 684{
808 struct sr030pc30_info *info; 685 struct sr030pc30_info *info;
809 struct v4l2_subdev *sd; 686 struct v4l2_subdev *sd;
687 struct v4l2_ctrl_handler *hdl;
810 const struct sr030pc30_platform_data *pdata 688 const struct sr030pc30_platform_data *pdata
811 = client->dev.platform_data; 689 = client->dev.platform_data;
812 int ret; 690 int ret;
@@ -830,10 +708,31 @@ static int sr030pc30_probe(struct i2c_client *client,
830 708
831 v4l2_i2c_subdev_init(sd, client, &sr030pc30_ops); 709 v4l2_i2c_subdev_init(sd, client, &sr030pc30_ops);
832 710
711 hdl = &info->hdl;
712 v4l2_ctrl_handler_init(hdl, 6);
713 info->awb = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
714 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
715 info->red = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
716 V4L2_CID_RED_BALANCE, 0, 127, 1, 64);
717 info->blue = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
718 V4L2_CID_BLUE_BALANCE, 0, 127, 1, 64);
719 info->autoexp = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
720 V4L2_CID_EXPOSURE_AUTO, 0, 1, 1, 1);
721 info->exp = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
722 V4L2_CID_EXPOSURE, EXPOS_MIN_MS, EXPOS_MAX_MS, 1, 30);
723 sd->ctrl_handler = hdl;
724 if (hdl->error) {
725 int err = hdl->error;
726
727 v4l2_ctrl_handler_free(hdl);
728 return err;
729 }
730 v4l2_ctrl_auto_cluster(3, &info->awb, 0, false);
731 v4l2_ctrl_auto_cluster(2, &info->autoexp, V4L2_EXPOSURE_MANUAL, false);
732 v4l2_ctrl_handler_setup(hdl);
733
833 info->i2c_reg_page = -1; 734 info->i2c_reg_page = -1;
834 info->hflip = 1; 735 info->hflip = 1;
835 info->auto_exp = 1;
836 info->exposure = 30;
837 736
838 return 0; 737 return 0;
839} 738}
@@ -843,6 +742,7 @@ static int sr030pc30_remove(struct i2c_client *client)
843 struct v4l2_subdev *sd = i2c_get_clientdata(client); 742 struct v4l2_subdev *sd = i2c_get_clientdata(client);
844 743
845 v4l2_device_unregister_subdev(sd); 744 v4l2_device_unregister_subdev(sd);
745 v4l2_ctrl_handler_free(sd->ctrl_handler);
846 return 0; 746 return 0;
847} 747}
848 748