media: ov2740: only do OTP data read on demand from user
PATCH 1/2 media: ov2740: only do OTP data read on demand from user
OTP data access of ov2740 in probe need power up, it may cause the camera flash LED blink during probe if the LED use same power rail with camera, this patch move the OTP data access out of probe, it will only occur on demand from user by nvmem sysfs.
--- drivers/media/i2c/ov2740.c | 63 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 15 deletions(-)
drivers/media/i2c/ov2740.c +48 -15
@@ -71,9 +71,11 @@#define OV2740_REG_OTP_CUSTOMER 0x7010struct nvm_data { -char *nvm_buffer; +struct i2c_client *client;struct nvmem_device *nvmem;struct regmap *regmap; +char *nvm_buffer; +bool ready; };enum {
@@ -335,6 +337,9 @@ struct ov2740 {/* Streaming on/off */ bool streaming; + +/* NVM data inforamtion */ +struct nvm_data *nvm; };static inline struct ov2740 *to_ov2740 (struct v4l2_subdev *subdev)
@@ -930,8 +935,9 @@ static int ov2740_remove(struct i2c_client *client)return 0 ; } -static int ov2740_load_otp_data (struct i2c_client *client,struct nvm_data *nvm) +static int ov2740_load_otp_data (struct nvm_data *nvm) { +struct i2c_client *client = nvm->client;struct ov2740 *ov2740 =to_ov2740 (i2c_get_clientdata (client));u32 isp_ctrl00 =0 ;u32 isp_ctrl01 =0 ;
@@ -967,7 +973,7 @@ static int ov2740_load_otp_data(struct i2c_client *client, struct nvm_data *nvm) ret =ov2740_write_reg (ov2740,OV2740_REG_MODE_SELECT ,1 ,OV2740_MODE_STREAMING );if (ret) { -dev_err (&client->dev,"failed to start streaming\n" ); +dev_err (&client->dev,"failed to set streaming mode\n" ); goto exit; }
@@ -997,20 +1003,48 @@ static int ov2740_nvmem_read(void *priv, unsigned int off, void *val, size_t count) {struct nvm_data *nvm = priv; +struct v4l2_subdev *sd =i2c_get_clientdata (nvm->client); +struct device *dev = &nvm->client->dev; +struct ov2740 *ov2740 =to_ov2740 (sd); +int ret =0 ; + +mutex_lock (&ov2740->mutex); -memcpy (val, nvm->nvm_buffer + off, count); +if (!nvm->ready) { +if (ov2740->streaming) { + ret = -EBUSY ; + goto exit; + } -return 0 ; + ret =pm_runtime_get_sync (dev); +if (ret <0 ) { +pm_runtime_put_noidle (dev); + goto exit; + } + ret =ov2740_load_otp_data (nvm); +if (!ret) + nvm->ready = true; +pm_runtime_put (dev); + } + +exit: +mutex_unlock (&ov2740->mutex); + +if (!ret) +memcpy (val, nvm->nvm_buffer + off, count); + +return ret; } -static int ov2740_register_nvmem (struct i2c_client *client) +static int ov2740_register_nvmem (struct i2c_client *client, +struct ov2740 *ov2740) {struct nvm_data *nvm;struct regmap_config regmap_config = { };struct nvmem_config nvmem_config = { };struct regmap *regmap;struct device *dev = &client->dev; -int ret =0 ; +int ret; nvm =devm_kzalloc (dev,sizeof (*nvm),GFP_KERNEL );if (!nvm)
@@ -1028,12 +1062,7 @@ static int ov2740_register_nvmem(struct i2c_client *client)return PTR_ERR (regmap); nvm->regmap = regmap; - - ret =ov2740_load_otp_data (client, nvm); -if (ret) { -dev_err (dev,"failed to load OTP data, ret %d\n" , ret); -return ret; - } + nvm->client = client; nvmem_config.name =dev_name (dev); nvmem_config.dev = dev;
@@ -1051,7 +1080,11 @@ static int ov2740_register_nvmem(struct i2c_client *client) nvm->nvmem =devm_nvmem_register (dev, &nvmem_config); -return PTR_ERR_OR_ZERO (nvm->nvmem); + ret =PTR_ERR_OR_ZERO (nvm->nvmem); +if (!ret) + ov2740->nvm = nvm; + +return ret; }static int ov2740_probe (struct i2c_client *client)
@@ -1103,7 +1136,7 @@ static int ov2740_probe(struct i2c_client *client) goto probe_error_media_entity_cleanup; } - ret =ov2740_register_nvmem (client); + ret =ov2740_register_nvmem (client, ov2740);if (ret)dev_warn (&client->dev,"register nvmem failed, ret %d\n" , ret);
Signature · 1 line
2.7.4
PATCH 2/2 media: ov2740: allow OTP data access during streaming
OTP data access of ov2740 need enable the streaming mode to load and it could be done in any time, so driver need allow the OTP data access during streaming instead of return EBUSY, this patch try to read the OTP data out in STREAMON if OTP data is not ready before first time streaming start.
--- drivers/media/i2c/ov2740.c | 135 ++++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 64 deletions(-)
drivers/media/i2c/ov2740.c +71 -64
@@ -599,13 +599,84 @@ static void ov2740_update_pad_format(const struct ov2740_mode *mode, fmt->field =V4L2_FIELD_NONE ; } +static int ov2740_load_otp_data (struct nvm_data *nvm) +{ +struct i2c_client *client = nvm->client; +struct ov2740 *ov2740 =to_ov2740 (i2c_get_clientdata (client)); +u32 isp_ctrl00 =0 ; +u32 isp_ctrl01 =0 ; +int ret; + + ret =ov2740_read_reg (ov2740,OV2740_REG_ISP_CTRL00 ,1 , &isp_ctrl00); +if (ret) { +dev_err (&client->dev,"failed to read ISP CTRL00\n" ); + goto exit; + } + ret =ov2740_read_reg (ov2740,OV2740_REG_ISP_CTRL01 ,1 , &isp_ctrl01); +if (ret) { +dev_err (&client->dev,"failed to read ISP CTRL01\n" ); + goto exit; + } + +/* Clear bit 5 of ISP CTRL00 */ + ret =ov2740_write_reg (ov2740,OV2740_REG_ISP_CTRL00 ,1 , + isp_ctrl00 & ~BIT (5 )); +if (ret) { +dev_err (&client->dev,"failed to write ISP CTRL00\n" ); + goto exit; + } + +/* Clear bit 7 of ISP CTRL01 */ + ret =ov2740_write_reg (ov2740,OV2740_REG_ISP_CTRL01 ,1 , + isp_ctrl01 & ~BIT (7 )); +if (ret) { +dev_err (&client->dev,"failed to write ISP CTRL01\n" ); + goto exit; + } + + ret =ov2740_write_reg (ov2740,OV2740_REG_MODE_SELECT ,1 , +OV2740_MODE_STREAMING ); +if (ret) { +dev_err (&client->dev,"failed to set streaming mode\n" ); + goto exit; + } + +/* +* Users are not allowed to access OTP-related registers and memory +* during the 20 ms period after streaming starts (0x100 = 0x01). +*/ +msleep (20 ); + + ret =regmap_bulk_read (nvm->regmap,OV2740_REG_OTP_CUSTOMER , + nvm->nvm_buffer,CUSTOMER_USE_OTP_SIZE ); +if (ret) { +dev_err (&client->dev,"failed to read OTP data, ret %d\n" , ret); + goto exit; + } + +ov2740_write_reg (ov2740,OV2740_REG_MODE_SELECT ,1 , +OV2740_MODE_STANDBY ); +ov2740_write_reg (ov2740,OV2740_REG_ISP_CTRL01 ,1 , isp_ctrl01); +ov2740_write_reg (ov2740,OV2740_REG_ISP_CTRL00 ,1 , isp_ctrl00); + +exit: +return ret; +} +static int ov2740_start_streaming (struct ov2740 *ov2740) {struct i2c_client *client =v4l2_get_subdevdata (&ov2740->sd); +struct nvm_data *nvm = ov2740->nvm;const struct ov2740_reg_list *reg_list;int link_freq_index;int ret =0 ; +if (nvm && !nvm->ready) { + ret =ov2740_load_otp_data (nvm); +if (!ret) + nvm->ready = true; + } + link_freq_index = ov2740->cur_mode->link_freq_index; reg_list = &link_freq_configs[link_freq_index].reg_list; ret =ov2740_write_reg_list (ov2740, reg_list);
@@ -935,70 +1006,6 @@ static int ov2740_remove(struct i2c_client *client)return 0 ; } -static int ov2740_load_otp_data (struct nvm_data *nvm) -{ -struct i2c_client *client = nvm->client; -struct ov2740 *ov2740 =to_ov2740 (i2c_get_clientdata (client)); -u32 isp_ctrl00 =0 ; -u32 isp_ctrl01 =0 ; -int ret; - - ret =ov2740_read_reg (ov2740,OV2740_REG_ISP_CTRL00 ,1 , &isp_ctrl00); -if (ret) { -dev_err (&client->dev,"failed to read ISP CTRL00\n" ); - goto exit; - } - ret =ov2740_read_reg (ov2740,OV2740_REG_ISP_CTRL01 ,1 , &isp_ctrl01); -if (ret) { -dev_err (&client->dev,"failed to read ISP CTRL01\n" ); - goto exit; - } - -/* Clear bit 5 of ISP CTRL00 */ - ret =ov2740_write_reg (ov2740,OV2740_REG_ISP_CTRL00 ,1 , - isp_ctrl00 & ~BIT (5 )); -if (ret) { -dev_err (&client->dev,"failed to write ISP CTRL00\n" ); - goto exit; - } - -/* Clear bit 7 of ISP CTRL01 */ - ret =ov2740_write_reg (ov2740,OV2740_REG_ISP_CTRL01 ,1 , - isp_ctrl01 & ~BIT (7 )); -if (ret) { -dev_err (&client->dev,"failed to write ISP CTRL01\n" ); - goto exit; - } - - ret =ov2740_write_reg (ov2740,OV2740_REG_MODE_SELECT ,1 , -OV2740_MODE_STREAMING ); -if (ret) { -dev_err (&client->dev,"failed to set streaming mode\n" ); - goto exit; - } - -/* -* Users are not allowed to access OTP-related registers and memory -* during the 20 ms period after streaming starts (0x100 = 0x01). -*/ -msleep (20 ); - - ret =regmap_bulk_read (nvm->regmap,OV2740_REG_OTP_CUSTOMER , - nvm->nvm_buffer,CUSTOMER_USE_OTP_SIZE ); -if (ret) { -dev_err (&client->dev,"failed to read OTP data, ret %d\n" , ret); - goto exit; - } - -ov2740_write_reg (ov2740,OV2740_REG_MODE_SELECT ,1 , -OV2740_MODE_STANDBY ); -ov2740_write_reg (ov2740,OV2740_REG_ISP_CTRL01 ,1 , isp_ctrl01); -ov2740_write_reg (ov2740,OV2740_REG_ISP_CTRL00 ,1 , isp_ctrl00); - -exit: -return ret; -} -static int ov2740_nvmem_read (void *priv,unsigned int off,void *val,size_t count) {
Signature · 1 line
2.7.4
Sakari Ailus
maintainerHi Bingbu,
Thanks for the patch.
Quoted 67 lines · Bingbu Cao
OTP data access of ov2740 in probe need power up, it may cause the camera flash LED blink during probe if the LED use same power rail with camera, this patch move the OTP data access out of probe, it will only occur on demand from user by nvmem sysfs.
Signed-off-by Bingbu Cao <[email protected]>Signed-off-by Qingwu Zhang <[email protected]>--- drivers/media/i2c/ov2740.c | 63 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 15 deletions(-)drivers/media/i2c/ov2740.c +14
-3@@ -71,9 +71,11 @@#define OV2740_REG_OTP_CUSTOMER 0x7010struct nvm_data { -char *nvm_buffer; +struct i2c_client *client;struct nvmem_device *nvmem;struct regmap *regmap; +char *nvm_buffer; +bool ready; };enum {@@ -335,6 +337,9 @@ struct ov2740 {/* Streaming on/off */ bool streaming; + +/* NVM data inforamtion */ +struct nvm_data *nvm; };static inline struct ov2740 *to_ov2740 (struct v4l2_subdev *subdev)@@ -930,8 +935,9 @@ static int ov2740_remove(struct i2c_client *client)return 0 ; } -static int ov2740_load_otp_data (struct i2c_client *client,struct nvm_data *nvm) +static int ov2740_load_otp_data (struct nvm_data *nvm) { +struct i2c_client *client = nvm->client;struct ov2740 *ov2740 =to_ov2740 (i2c_get_clientdata (client));u32 isp_ctrl00 =0 ;u32 isp_ctrl01 =0 ;@@ -967,7 +973,7 @@ static int ov2740_load_otp_data(struct i2c_client *client, struct nvm_data *nvm) ret =ov2740_write_reg (ov2740,OV2740_REG_MODE_SELECT ,1 ,OV2740_MODE_STREAMING );if (ret) { -dev_err (&client->dev,"failed to start streaming\n" ); +dev_err (&client->dev,"failed to set streaming mode\n" ); goto exit; }@@ -997,20 +1003,48 @@ static int ov2740_nvmem_read(void *priv, unsigned int off, void *val, size_t count) {struct nvm_data *nvm = priv; +struct v4l2_subdev *sd =i2c_get_clientdata (nvm->client); +struct device *dev = &nvm->client->dev; +struct ov2740 *ov2740 =to_ov2740 (sd); +int ret =0 ; +
drivers/media/i2c/ov2740.c static int ov2740_nvmem_read(void *priv, unsigned int off, void *val, · line 1003
Quoted 10 lines
drivers/media/i2c/ov2740.c +6
-2+mutex_lock (&ov2740->mutex); -memcpy (val, nvm->nvm_buffer + off, count); +if (!nvm->ready) { +if (ov2740->streaming) { + ret = -EBUSY ; + goto exit; + } -return 0 ;
If you also moved memory allocation here, you could omit the ready field. I think it'd be cleaner that way as well.
Quoted 70 lines
drivers/media/i2c/ov2740.c +28
-10+ ret =pm_runtime_get_sync (dev); +if (ret <0 ) { +pm_runtime_put_noidle (dev); + goto exit; + } + ret =ov2740_load_otp_data (nvm); +if (!ret) + nvm->ready = true; +pm_runtime_put (dev); + } + +exit: +mutex_unlock (&ov2740->mutex); + +if (!ret) +memcpy (val, nvm->nvm_buffer + off, count); + +return ret; } -static int ov2740_register_nvmem (struct i2c_client *client) +static int ov2740_register_nvmem (struct i2c_client *client, +struct ov2740 *ov2740) {struct nvm_data *nvm;struct regmap_config regmap_config = { };struct nvmem_config nvmem_config = { };struct regmap *regmap;struct device *dev = &client->dev; -int ret =0 ; +int ret; nvm =devm_kzalloc (dev,sizeof (*nvm),GFP_KERNEL );if (!nvm)@@ -1028,12 +1062,7 @@ static int ov2740_register_nvmem(struct i2c_client *client)return PTR_ERR (regmap); nvm->regmap = regmap; - - ret =ov2740_load_otp_data (client, nvm); -if (ret) { -dev_err (dev,"failed to load OTP data, ret %d\n" , ret); -return ret; - } + nvm->client = client; nvmem_config.name =dev_name (dev); nvmem_config.dev = dev;@@ -1051,7 +1080,11 @@ static int ov2740_register_nvmem(struct i2c_client *client) nvm->nvmem =devm_nvmem_register (dev, &nvmem_config); -return PTR_ERR_OR_ZERO (nvm->nvmem); + ret =PTR_ERR_OR_ZERO (nvm->nvmem); +if (!ret) + ov2740->nvm = nvm; + +return ret; }static int ov2740_probe (struct i2c_client *client)@@ -1103,7 +1136,7 @@ static int ov2740_probe(struct i2c_client *client) goto probe_error_media_entity_cleanup; } - ret =ov2740_register_nvmem (client); + ret =ov2740_register_nvmem (client, ov2740);if (ret)dev_warn (&client->dev,"register nvmem failed, ret %d\n" , ret);
Signature · 3 lines
Regards, Sakari Ailus
Sakari, thanks for your review.
Quoted 81 lines · Sakari Ailus
Hi Bingbu,
Thanks for the patch.
Quoted 76 lines · Bingbu Cao
OTP data access of ov2740 in probe need power up, it may cause the camera flash LED blink during probe if the LED use same power rail with camera, this patch move the OTP data access out of probe, it will only occur on demand from user by nvmem sysfs.
Signed-off-by Bingbu Cao <[email protected]>Signed-off-by Qingwu Zhang <[email protected]>--- drivers/media/i2c/ov2740.c | 63 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 15 deletions(-)drivers/media/i2c/ov2740.c +20
-4@@ -71,9 +71,11 @@#define OV2740_REG_OTP_CUSTOMER 0x7010struct nvm_data { -char *nvm_buffer; +struct i2c_client *client;struct nvmem_device *nvmem;struct regmap *regmap; +char *nvm_buffer; +bool ready; };enum {@@ -335,6 +337,9 @@ struct ov2740 {/* Streaming on/off */ bool streaming; + +/* NVM data inforamtion */ +struct nvm_data *nvm; };static inline struct ov2740 *to_ov2740 (struct v4l2_subdev *subdev)@@ -930,8 +935,9 @@ static int ov2740_remove(struct i2c_client *client)return 0 ; } -static int ov2740_load_otp_data (struct i2c_client *client,struct nvm_data *nvm) +static int ov2740_load_otp_data (struct nvm_data *nvm) { +struct i2c_client *client = nvm->client;struct ov2740 *ov2740 =to_ov2740 (i2c_get_clientdata (client));u32 isp_ctrl00 =0 ;u32 isp_ctrl01 =0 ;@@ -967,7 +973,7 @@ static int ov2740_load_otp_data(struct i2c_client *client, struct nvm_data *nvm) ret =ov2740_write_reg (ov2740,OV2740_REG_MODE_SELECT ,1 ,OV2740_MODE_STREAMING );if (ret) { -dev_err (&client->dev,"failed to start streaming\n" ); +dev_err (&client->dev,"failed to set streaming mode\n" ); goto exit; }@@ -997,20 +1003,48 @@ static int ov2740_nvmem_read(void *priv, unsigned int off, void *val, size_t count) {struct nvm_data *nvm = priv; +struct v4l2_subdev *sd =i2c_get_clientdata (nvm->client); +struct device *dev = &nvm->client->dev; +struct ov2740 *ov2740 =to_ov2740 (sd); +int ret =0 ; + +mutex_lock (&ov2740->mutex); -memcpy (val, nvm->nvm_buffer + off, count); +if (!nvm->ready) { +if (ov2740->streaming) { + ret = -EBUSY ; + goto exit; + }
Quoted 4 lines
drivers/media/i2c/ov2740.c static int ov2740_nvmem_read(void *priv, unsigned int off, void *val, · line 1003
Quoted 1 line
drivers/media/i2c/ov2740.c +0
-1-return 0 ;If you also moved memory allocation here, you could omit the ready field. I think it'd be cleaner that way as well.
The 'ready' field was also used to mark the OTP data loading during streaming, in patch 2/2. Your comments inspired me, I think I could move the ready checking into OTP data read in v2.
Quoted 72 lines
Quoted 71 lines
drivers/media/i2c/ov2740.c +28
-10+ ret =pm_runtime_get_sync (dev); +if (ret <0 ) { +pm_runtime_put_noidle (dev); + goto exit; + } + ret =ov2740_load_otp_data (nvm); +if (!ret) + nvm->ready = true; +pm_runtime_put (dev); + } + +exit: +mutex_unlock (&ov2740->mutex); + +if (!ret) +memcpy (val, nvm->nvm_buffer + off, count); + +return ret; } -static int ov2740_register_nvmem (struct i2c_client *client) +static int ov2740_register_nvmem (struct i2c_client *client, +struct ov2740 *ov2740) {struct nvm_data *nvm;struct regmap_config regmap_config = { };struct nvmem_config nvmem_config = { };struct regmap *regmap;struct device *dev = &client->dev; -int ret =0 ; +int ret; nvm =devm_kzalloc (dev,sizeof (*nvm),GFP_KERNEL );if (!nvm)@@ -1028,12 +1062,7 @@ static int ov2740_register_nvmem(struct i2c_client *client)return PTR_ERR (regmap); nvm->regmap = regmap; - - ret =ov2740_load_otp_data (client, nvm); -if (ret) { -dev_err (dev,"failed to load OTP data, ret %d\n" , ret); -return ret; - } + nvm->client = client; nvmem_config.name =dev_name (dev); nvmem_config.dev = dev;@@ -1051,7 +1080,11 @@ static int ov2740_register_nvmem(struct i2c_client *client) nvm->nvmem =devm_nvmem_register (dev, &nvmem_config); -return PTR_ERR_OR_ZERO (nvm->nvmem); + ret =PTR_ERR_OR_ZERO (nvm->nvmem); +if (!ret) + ov2740->nvm = nvm; + +return ret; }static int ov2740_probe (struct i2c_client *client)@@ -1103,7 +1136,7 @@ static int ov2740_probe(struct i2c_client *client) goto probe_error_media_entity_cleanup; } - ret =ov2740_register_nvmem (client); + ret =ov2740_register_nvmem (client, ov2740);if (ret)dev_warn (&client->dev,"register nvmem failed, ret %d\n" , ret);
Signature · 2 lines
Best regards, Bingbu Cao