Lines Matching full:mt9p031
3 * Driver for MT9P031 CMOS Image Sensor from Aptina
26 #include <media/i2c/mt9p031.h>
120 struct mt9p031 { struct
147 static struct mt9p031 *to_mt9p031(struct v4l2_subdev *sd) in to_mt9p031() argument
149 return container_of(sd, struct mt9p031, subdev); in to_mt9p031()
162 static int mt9p031_set_output_control(struct mt9p031 *mt9p031, u16 clear, in mt9p031_set_output_control() argument
165 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_output_control()
166 u16 value = (mt9p031->output_control & ~clear) | set; in mt9p031_set_output_control()
173 mt9p031->output_control = value; in mt9p031_set_output_control()
177 static int mt9p031_set_mode2(struct mt9p031 *mt9p031, u16 clear, u16 set) in mt9p031_set_mode2() argument
179 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_mode2()
180 u16 value = (mt9p031->mode2 & ~clear) | set; in mt9p031_set_mode2()
187 mt9p031->mode2 = value; in mt9p031_set_mode2()
191 static int mt9p031_reset(struct mt9p031 *mt9p031) in mt9p031_reset() argument
193 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_reset()
205 MT9P031_PIXEL_CLOCK_DIVIDE(mt9p031->clk_div)); in mt9p031_reset()
209 return mt9p031_set_output_control(mt9p031, MT9P031_OUTPUT_CONTROL_CEN, in mt9p031_reset()
213 static int mt9p031_clk_setup(struct mt9p031 *mt9p031) in mt9p031_clk_setup() argument
231 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_clk_setup()
232 struct mt9p031_platform_data *pdata = mt9p031->pdata; in mt9p031_clk_setup()
236 mt9p031->clk = devm_clk_get(&client->dev, NULL); in mt9p031_clk_setup()
237 if (IS_ERR(mt9p031->clk)) in mt9p031_clk_setup()
238 return PTR_ERR(mt9p031->clk); in mt9p031_clk_setup()
240 ret = clk_set_rate(mt9p031->clk, pdata->ext_freq); in mt9p031_clk_setup()
244 ext_freq = clk_get_rate(mt9p031->clk); in mt9p031_clk_setup()
255 mt9p031->clk_div = min_t(unsigned int, div, 64); in mt9p031_clk_setup()
256 mt9p031->use_pll = false; in mt9p031_clk_setup()
261 mt9p031->pll.ext_clock = ext_freq; in mt9p031_clk_setup()
262 mt9p031->pll.pix_clock = pdata->target_freq; in mt9p031_clk_setup()
263 mt9p031->use_pll = true; in mt9p031_clk_setup()
265 return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll); in mt9p031_clk_setup()
268 static int mt9p031_pll_enable(struct mt9p031 *mt9p031) in mt9p031_pll_enable() argument
270 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_pll_enable()
273 if (!mt9p031->use_pll) in mt9p031_pll_enable()
282 (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1)); in mt9p031_pll_enable()
286 ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1); in mt9p031_pll_enable()
297 static inline int mt9p031_pll_disable(struct mt9p031 *mt9p031) in mt9p031_pll_disable() argument
299 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_pll_disable()
301 if (!mt9p031->use_pll) in mt9p031_pll_disable()
308 static int mt9p031_power_on(struct mt9p031 *mt9p031) in mt9p031_power_on() argument
314 if (mt9p031->reset) { in mt9p031_power_on()
315 gpiod_set_value(mt9p031->reset, 1); in mt9p031_power_on()
320 ret = regulator_bulk_enable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_on()
321 mt9p031->regulators); in mt9p031_power_on()
326 if (mt9p031->clk) { in mt9p031_power_on()
327 ret = clk_prepare_enable(mt9p031->clk); in mt9p031_power_on()
329 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_on()
330 mt9p031->regulators); in mt9p031_power_on()
336 if (mt9p031->reset) { in mt9p031_power_on()
337 gpiod_set_value(mt9p031->reset, 0); in mt9p031_power_on()
339 rate = clk_get_rate(mt9p031->clk); in mt9p031_power_on()
349 static void mt9p031_power_off(struct mt9p031 *mt9p031) in mt9p031_power_off() argument
351 if (mt9p031->reset) { in mt9p031_power_off()
352 gpiod_set_value(mt9p031->reset, 1); in mt9p031_power_off()
356 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_off()
357 mt9p031->regulators); in mt9p031_power_off()
359 clk_disable_unprepare(mt9p031->clk); in mt9p031_power_off()
362 static int __mt9p031_set_power(struct mt9p031 *mt9p031, bool on) in __mt9p031_set_power() argument
364 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in __mt9p031_set_power()
368 mt9p031_power_off(mt9p031); in __mt9p031_set_power()
372 ret = mt9p031_power_on(mt9p031); in __mt9p031_set_power()
376 ret = mt9p031_reset(mt9p031); in __mt9p031_set_power()
383 if (mt9p031->pdata && mt9p031->pdata->pixclk_pol) { in __mt9p031_set_power()
390 return v4l2_ctrl_handler_setup(&mt9p031->ctrls); in __mt9p031_set_power()
397 static int mt9p031_set_params(struct mt9p031 *mt9p031) in mt9p031_set_params() argument
399 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_params()
400 struct v4l2_mbus_framefmt *format = &mt9p031->format; in mt9p031_set_params()
401 const struct v4l2_rect *crop = &mt9p031->crop; in mt9p031_set_params()
464 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_s_stream() local
483 ret = mt9p031_set_output_control(mt9p031, in mt9p031_s_stream()
488 return mt9p031_pll_disable(mt9p031); in mt9p031_s_stream()
491 ret = mt9p031_set_params(mt9p031); in mt9p031_s_stream()
496 ret = mt9p031_set_output_control(mt9p031, 0, in mt9p031_s_stream()
511 return mt9p031_pll_enable(mt9p031); in mt9p031_s_stream()
518 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_enum_mbus_code() local
523 code->code = mt9p031->format.code; in mt9p031_enum_mbus_code()
531 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_enum_frame_size() local
533 if (fse->index >= 8 || fse->code != mt9p031->format.code) in mt9p031_enum_frame_size()
546 __mt9p031_get_pad_format(struct mt9p031 *mt9p031, in __mt9p031_get_pad_format() argument
552 return v4l2_subdev_get_try_format(&mt9p031->subdev, sd_state, in __mt9p031_get_pad_format()
555 return &mt9p031->format; in __mt9p031_get_pad_format()
562 __mt9p031_get_pad_crop(struct mt9p031 *mt9p031, in __mt9p031_get_pad_crop() argument
568 return v4l2_subdev_get_try_crop(&mt9p031->subdev, sd_state, in __mt9p031_get_pad_crop()
571 return &mt9p031->crop; in __mt9p031_get_pad_crop()
581 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_get_format() local
583 fmt->format = *__mt9p031_get_pad_format(mt9p031, sd_state, fmt->pad, in mt9p031_get_format()
592 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_set_format() local
600 __crop = __mt9p031_get_pad_crop(mt9p031, sd_state, format->pad, in mt9p031_set_format()
616 __format = __mt9p031_get_pad_format(mt9p031, sd_state, format->pad, in mt9p031_set_format()
630 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_get_selection() local
641 sel->r = *__mt9p031_get_pad_crop(mt9p031, sd_state, in mt9p031_get_selection()
654 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_set_selection() local
681 __crop = __mt9p031_get_pad_crop(mt9p031, sd_state, sel->pad, in mt9p031_set_selection()
688 __format = __mt9p031_get_pad_format(mt9p031, sd_state, in mt9p031_set_selection()
704 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_init_cfg() local
710 crop = __mt9p031_get_pad_crop(mt9p031, sd_state, 0, which); in mt9p031_init_cfg()
716 format = __mt9p031_get_pad_format(mt9p031, sd_state, 0, which); in mt9p031_init_cfg()
718 if (mt9p031->model == MT9P031_MODEL_MONOCHROME) in mt9p031_init_cfg()
740 static int mt9p031_restore_blc(struct mt9p031 *mt9p031) in mt9p031_restore_blc() argument
742 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_restore_blc()
745 if (mt9p031->blc_auto->cur.val != 0) { in mt9p031_restore_blc()
746 ret = mt9p031_set_mode2(mt9p031, 0, in mt9p031_restore_blc()
752 if (mt9p031->blc_offset->cur.val != 0) { in mt9p031_restore_blc()
754 mt9p031->blc_offset->cur.val); in mt9p031_restore_blc()
764 struct mt9p031 *mt9p031 = in mt9p031_s_ctrl() local
765 container_of(ctrl->handler, struct mt9p031, ctrls); in mt9p031_s_ctrl()
766 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_s_ctrl()
812 return mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
815 return mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
820 return mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
823 return mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
832 v4l2_ctrl_activate(mt9p031->blc_auto, ctrl->val == 0); in mt9p031_s_ctrl()
833 v4l2_ctrl_activate(mt9p031->blc_offset, ctrl->val == 0); in mt9p031_s_ctrl()
837 ret = mt9p031_restore_blc(mt9p031); in mt9p031_s_ctrl()
855 ret = mt9p031_set_mode2(mt9p031, MT9P031_READ_MODE_2_ROW_BLC, in mt9p031_s_ctrl()
869 ret = mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
971 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_set_power() local
974 mutex_lock(&mt9p031->power_lock); in mt9p031_set_power()
979 if (mt9p031->power_count == !on) { in mt9p031_set_power()
980 ret = __mt9p031_set_power(mt9p031, !!on); in mt9p031_set_power()
986 mt9p031->power_count += on ? 1 : -1; in mt9p031_set_power()
987 WARN_ON(mt9p031->power_count < 0); in mt9p031_set_power()
990 mutex_unlock(&mt9p031->power_lock); in mt9p031_set_power()
1001 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_registered() local
1005 ret = mt9p031_power_on(mt9p031); in mt9p031_registered()
1007 dev_err(&client->dev, "MT9P031 power up failed\n"); in mt9p031_registered()
1013 mt9p031_power_off(mt9p031); in mt9p031_registered()
1016 dev_err(&client->dev, "MT9P031 not detected, wrong version " in mt9p031_registered()
1021 dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n", in mt9p031_registered()
1110 struct mt9p031 *mt9p031; in mt9p031_probe() local
1125 mt9p031 = devm_kzalloc(&client->dev, sizeof(*mt9p031), GFP_KERNEL); in mt9p031_probe()
1126 if (mt9p031 == NULL) in mt9p031_probe()
1129 mt9p031->pdata = pdata; in mt9p031_probe()
1130 mt9p031->output_control = MT9P031_OUTPUT_CONTROL_DEF; in mt9p031_probe()
1131 mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC; in mt9p031_probe()
1132 mt9p031->model = did->driver_data; in mt9p031_probe()
1134 mt9p031->regulators[0].supply = "vdd"; in mt9p031_probe()
1135 mt9p031->regulators[1].supply = "vdd_io"; in mt9p031_probe()
1136 mt9p031->regulators[2].supply = "vaa"; in mt9p031_probe()
1138 ret = devm_regulator_bulk_get(&client->dev, 3, mt9p031->regulators); in mt9p031_probe()
1144 mutex_init(&mt9p031->power_lock); in mt9p031_probe()
1146 v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 6); in mt9p031_probe()
1148 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1152 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1155 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1157 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1159 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1162 v4l2_ctrl_new_std_menu_items(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1168 v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL); in mt9p031_probe()
1170 mt9p031->subdev.ctrl_handler = &mt9p031->ctrls; in mt9p031_probe()
1172 if (mt9p031->ctrls.error) { in mt9p031_probe()
1174 __func__, mt9p031->ctrls.error); in mt9p031_probe()
1175 ret = mt9p031->ctrls.error; in mt9p031_probe()
1179 mt9p031->blc_auto = v4l2_ctrl_find(&mt9p031->ctrls, V4L2_CID_BLC_AUTO); in mt9p031_probe()
1180 mt9p031->blc_offset = v4l2_ctrl_find(&mt9p031->ctrls, in mt9p031_probe()
1183 v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops); in mt9p031_probe()
1184 mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops; in mt9p031_probe()
1186 mt9p031->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; in mt9p031_probe()
1187 mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE; in mt9p031_probe()
1188 ret = media_entity_pads_init(&mt9p031->subdev.entity, 1, &mt9p031->pad); in mt9p031_probe()
1192 mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in mt9p031_probe()
1194 ret = mt9p031_init_cfg(&mt9p031->subdev, NULL); in mt9p031_probe()
1198 mt9p031->reset = devm_gpiod_get_optional(&client->dev, "reset", in mt9p031_probe()
1201 ret = mt9p031_clk_setup(mt9p031); in mt9p031_probe()
1205 ret = v4l2_async_register_subdev(&mt9p031->subdev); in mt9p031_probe()
1209 v4l2_ctrl_handler_free(&mt9p031->ctrls); in mt9p031_probe()
1210 media_entity_cleanup(&mt9p031->subdev.entity); in mt9p031_probe()
1211 mutex_destroy(&mt9p031->power_lock); in mt9p031_probe()
1220 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_remove() local
1222 v4l2_ctrl_handler_free(&mt9p031->ctrls); in mt9p031_remove()
1225 mutex_destroy(&mt9p031->power_lock); in mt9p031_remove()
1230 { "mt9p031", MT9P031_MODEL_COLOR },
1239 { .compatible = "aptina,mt9p031", },
1249 .name = "mt9p031",
1258 MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");