Lines Matching full:mt9p031
3 * Driver for MT9P031 CMOS Image Sensor from Aptina
27 #include <media/i2c/mt9p031.h>
116 struct mt9p031 { struct
143 static struct mt9p031 *to_mt9p031(struct v4l2_subdev *sd) in to_mt9p031() argument
145 return container_of(sd, struct mt9p031, subdev); in to_mt9p031()
158 static int mt9p031_set_output_control(struct mt9p031 *mt9p031, u16 clear, in mt9p031_set_output_control() argument
161 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_output_control()
162 u16 value = (mt9p031->output_control & ~clear) | set; in mt9p031_set_output_control()
169 mt9p031->output_control = value; in mt9p031_set_output_control()
173 static int mt9p031_set_mode2(struct mt9p031 *mt9p031, u16 clear, u16 set) in mt9p031_set_mode2() argument
175 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_mode2()
176 u16 value = (mt9p031->mode2 & ~clear) | set; in mt9p031_set_mode2()
183 mt9p031->mode2 = value; in mt9p031_set_mode2()
187 static int mt9p031_reset(struct mt9p031 *mt9p031) in mt9p031_reset() argument
189 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_reset()
201 MT9P031_PIXEL_CLOCK_DIVIDE(mt9p031->clk_div)); in mt9p031_reset()
205 return mt9p031_set_output_control(mt9p031, MT9P031_OUTPUT_CONTROL_CEN, in mt9p031_reset()
209 static int mt9p031_clk_setup(struct mt9p031 *mt9p031) in mt9p031_clk_setup() argument
227 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_clk_setup()
228 struct mt9p031_platform_data *pdata = mt9p031->pdata; in mt9p031_clk_setup()
232 mt9p031->clk = devm_clk_get(&client->dev, NULL); in mt9p031_clk_setup()
233 if (IS_ERR(mt9p031->clk)) in mt9p031_clk_setup()
234 return PTR_ERR(mt9p031->clk); in mt9p031_clk_setup()
236 ret = clk_set_rate(mt9p031->clk, pdata->ext_freq); in mt9p031_clk_setup()
240 ext_freq = clk_get_rate(mt9p031->clk); in mt9p031_clk_setup()
251 mt9p031->clk_div = min_t(unsigned int, div, 64); in mt9p031_clk_setup()
252 mt9p031->use_pll = false; in mt9p031_clk_setup()
257 mt9p031->pll.ext_clock = ext_freq; in mt9p031_clk_setup()
258 mt9p031->pll.pix_clock = pdata->target_freq; in mt9p031_clk_setup()
259 mt9p031->use_pll = true; in mt9p031_clk_setup()
261 return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll); in mt9p031_clk_setup()
264 static int mt9p031_pll_enable(struct mt9p031 *mt9p031) in mt9p031_pll_enable() argument
266 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_pll_enable()
269 if (!mt9p031->use_pll) in mt9p031_pll_enable()
278 (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1)); in mt9p031_pll_enable()
282 ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1); in mt9p031_pll_enable()
293 static inline int mt9p031_pll_disable(struct mt9p031 *mt9p031) in mt9p031_pll_disable() argument
295 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_pll_disable()
297 if (!mt9p031->use_pll) in mt9p031_pll_disable()
304 static int mt9p031_power_on(struct mt9p031 *mt9p031) in mt9p031_power_on() argument
310 if (mt9p031->reset) { in mt9p031_power_on()
311 gpiod_set_value(mt9p031->reset, 1); in mt9p031_power_on()
316 ret = regulator_bulk_enable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_on()
317 mt9p031->regulators); in mt9p031_power_on()
322 if (mt9p031->clk) { in mt9p031_power_on()
323 ret = clk_prepare_enable(mt9p031->clk); in mt9p031_power_on()
325 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_on()
326 mt9p031->regulators); in mt9p031_power_on()
332 if (mt9p031->reset) { in mt9p031_power_on()
333 gpiod_set_value(mt9p031->reset, 0); in mt9p031_power_on()
335 rate = clk_get_rate(mt9p031->clk); in mt9p031_power_on()
345 static void mt9p031_power_off(struct mt9p031 *mt9p031) in mt9p031_power_off() argument
347 if (mt9p031->reset) { in mt9p031_power_off()
348 gpiod_set_value(mt9p031->reset, 1); in mt9p031_power_off()
352 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_off()
353 mt9p031->regulators); in mt9p031_power_off()
355 clk_disable_unprepare(mt9p031->clk); in mt9p031_power_off()
358 static int __mt9p031_set_power(struct mt9p031 *mt9p031, bool on) in __mt9p031_set_power() argument
360 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in __mt9p031_set_power()
364 mt9p031_power_off(mt9p031); in __mt9p031_set_power()
368 ret = mt9p031_power_on(mt9p031); in __mt9p031_set_power()
372 ret = mt9p031_reset(mt9p031); in __mt9p031_set_power()
379 if (mt9p031->pdata && mt9p031->pdata->pixclk_pol) { in __mt9p031_set_power()
386 return v4l2_ctrl_handler_setup(&mt9p031->ctrls); in __mt9p031_set_power()
393 static int mt9p031_set_params(struct mt9p031 *mt9p031) in mt9p031_set_params() argument
395 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_params()
396 struct v4l2_mbus_framefmt *format = &mt9p031->format; in mt9p031_set_params()
397 const struct v4l2_rect *crop = &mt9p031->crop; in mt9p031_set_params()
460 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_s_stream() local
479 ret = mt9p031_set_output_control(mt9p031, in mt9p031_s_stream()
484 return mt9p031_pll_disable(mt9p031); in mt9p031_s_stream()
487 ret = mt9p031_set_params(mt9p031); in mt9p031_s_stream()
492 ret = mt9p031_set_output_control(mt9p031, 0, in mt9p031_s_stream()
507 return mt9p031_pll_enable(mt9p031); in mt9p031_s_stream()
514 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_enum_mbus_code() local
519 code->code = mt9p031->format.code; in mt9p031_enum_mbus_code()
527 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_enum_frame_size() local
529 if (fse->index >= 8 || fse->code != mt9p031->format.code) in mt9p031_enum_frame_size()
542 __mt9p031_get_pad_format(struct mt9p031 *mt9p031, in __mt9p031_get_pad_format() argument
550 return &mt9p031->format; in __mt9p031_get_pad_format()
557 __mt9p031_get_pad_crop(struct mt9p031 *mt9p031, in __mt9p031_get_pad_crop() argument
565 return &mt9p031->crop; in __mt9p031_get_pad_crop()
575 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_get_format() local
577 fmt->format = *__mt9p031_get_pad_format(mt9p031, sd_state, fmt->pad, in mt9p031_get_format()
586 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_set_format() local
594 __crop = __mt9p031_get_pad_crop(mt9p031, sd_state, format->pad, in mt9p031_set_format()
610 __format = __mt9p031_get_pad_format(mt9p031, sd_state, format->pad, in mt9p031_set_format()
624 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_get_selection() local
635 sel->r = *__mt9p031_get_pad_crop(mt9p031, sd_state, in mt9p031_get_selection()
648 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_set_selection() local
675 __crop = __mt9p031_get_pad_crop(mt9p031, sd_state, sel->pad, in mt9p031_set_selection()
682 __format = __mt9p031_get_pad_format(mt9p031, sd_state, in mt9p031_set_selection()
698 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_init_state() local
704 crop = __mt9p031_get_pad_crop(mt9p031, sd_state, 0, which); in mt9p031_init_state()
710 format = __mt9p031_get_pad_format(mt9p031, sd_state, 0, which); in mt9p031_init_state()
711 format->code = mt9p031->code; in mt9p031_init_state()
729 static int mt9p031_restore_blc(struct mt9p031 *mt9p031) in mt9p031_restore_blc() argument
731 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_restore_blc()
734 if (mt9p031->blc_auto->cur.val != 0) { in mt9p031_restore_blc()
735 ret = mt9p031_set_mode2(mt9p031, 0, in mt9p031_restore_blc()
741 if (mt9p031->blc_offset->cur.val != 0) { in mt9p031_restore_blc()
743 mt9p031->blc_offset->cur.val); in mt9p031_restore_blc()
753 struct mt9p031 *mt9p031 = in mt9p031_s_ctrl() local
754 container_of(ctrl->handler, struct mt9p031, ctrls); in mt9p031_s_ctrl()
755 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_s_ctrl()
801 return mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
804 return mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
809 return mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
812 return mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
821 v4l2_ctrl_activate(mt9p031->blc_auto, ctrl->val == 0); in mt9p031_s_ctrl()
822 v4l2_ctrl_activate(mt9p031->blc_offset, ctrl->val == 0); in mt9p031_s_ctrl()
826 ret = mt9p031_restore_blc(mt9p031); in mt9p031_s_ctrl()
844 ret = mt9p031_set_mode2(mt9p031, MT9P031_READ_MODE_2_ROW_BLC, in mt9p031_s_ctrl()
858 ret = mt9p031_set_mode2(mt9p031, in mt9p031_s_ctrl()
960 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_set_power() local
963 mutex_lock(&mt9p031->power_lock); in mt9p031_set_power()
968 if (mt9p031->power_count == !on) { in mt9p031_set_power()
969 ret = __mt9p031_set_power(mt9p031, !!on); in mt9p031_set_power()
975 mt9p031->power_count += on ? 1 : -1; in mt9p031_set_power()
976 WARN_ON(mt9p031->power_count < 0); in mt9p031_set_power()
979 mutex_unlock(&mt9p031->power_lock); in mt9p031_set_power()
990 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_registered() local
994 ret = mt9p031_power_on(mt9p031); in mt9p031_registered()
996 dev_err(&client->dev, "MT9P031 power up failed\n"); in mt9p031_registered()
1002 mt9p031_power_off(mt9p031); in mt9p031_registered()
1005 dev_err(&client->dev, "MT9P031 not detected, wrong version " in mt9p031_registered()
1010 dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n", in mt9p031_registered()
1098 struct mt9p031 *mt9p031; in mt9p031_probe() local
1113 mt9p031 = devm_kzalloc(&client->dev, sizeof(*mt9p031), GFP_KERNEL); in mt9p031_probe()
1114 if (mt9p031 == NULL) in mt9p031_probe()
1117 mt9p031->pdata = pdata; in mt9p031_probe()
1118 mt9p031->output_control = MT9P031_OUTPUT_CONTROL_DEF; in mt9p031_probe()
1119 mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC; in mt9p031_probe()
1120 mt9p031->code = (uintptr_t)i2c_get_match_data(client); in mt9p031_probe()
1122 mt9p031->regulators[0].supply = "vdd"; in mt9p031_probe()
1123 mt9p031->regulators[1].supply = "vdd_io"; in mt9p031_probe()
1124 mt9p031->regulators[2].supply = "vaa"; in mt9p031_probe()
1126 ret = devm_regulator_bulk_get(&client->dev, 3, mt9p031->regulators); in mt9p031_probe()
1132 mutex_init(&mt9p031->power_lock); in mt9p031_probe()
1134 v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 6); in mt9p031_probe()
1136 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1140 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1143 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1145 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1147 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1150 v4l2_ctrl_new_std_menu_items(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1156 v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL); in mt9p031_probe()
1158 mt9p031->subdev.ctrl_handler = &mt9p031->ctrls; in mt9p031_probe()
1160 if (mt9p031->ctrls.error) { in mt9p031_probe()
1162 __func__, mt9p031->ctrls.error); in mt9p031_probe()
1163 ret = mt9p031->ctrls.error; in mt9p031_probe()
1167 mt9p031->blc_auto = v4l2_ctrl_find(&mt9p031->ctrls, V4L2_CID_BLC_AUTO); in mt9p031_probe()
1168 mt9p031->blc_offset = v4l2_ctrl_find(&mt9p031->ctrls, in mt9p031_probe()
1171 v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops); in mt9p031_probe()
1172 mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops; in mt9p031_probe()
1174 mt9p031->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; in mt9p031_probe()
1175 mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE; in mt9p031_probe()
1176 ret = media_entity_pads_init(&mt9p031->subdev.entity, 1, &mt9p031->pad); in mt9p031_probe()
1180 mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in mt9p031_probe()
1182 ret = mt9p031_init_state(&mt9p031->subdev, NULL); in mt9p031_probe()
1186 mt9p031->reset = devm_gpiod_get_optional(&client->dev, "reset", in mt9p031_probe()
1189 ret = mt9p031_clk_setup(mt9p031); in mt9p031_probe()
1193 ret = v4l2_async_register_subdev(&mt9p031->subdev); in mt9p031_probe()
1197 v4l2_ctrl_handler_free(&mt9p031->ctrls); in mt9p031_probe()
1198 media_entity_cleanup(&mt9p031->subdev.entity); in mt9p031_probe()
1199 mutex_destroy(&mt9p031->power_lock); in mt9p031_probe()
1208 struct mt9p031 *mt9p031 = to_mt9p031(subdev); in mt9p031_remove() local
1210 v4l2_ctrl_handler_free(&mt9p031->ctrls); in mt9p031_remove()
1213 mutex_destroy(&mt9p031->power_lock); in mt9p031_remove()
1218 { "mt9p031", MEDIA_BUS_FMT_SGRBG12_1X12 },
1226 { .compatible = "aptina,mt9p031", .data = (void *)MEDIA_BUS_FMT_SGRBG12_1X12 },
1235 .name = "mt9p031",
1244 MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");