media: i2c: add SCCB helpers
RFC PATCH 1/1 media: i2c: add SCCB helpers
(This patch is in prototype stage)
This adds SCCB helper functions (sccb_read_byte and sccb_write_byte) that are intended to be used by some of Omnivision sensor drivers.
The ov772x driver is going to use these functions in order to make it work with most i2c controllers.
As the ov772x device doesn't support repeated starts, this driver currently requires I2C_FUNC_PROTOCOL_MANGLING that is not supported by many i2c controller drivers.
With the sccb_read_byte() that issues two separated requests in order to avoid repeated start, the driver doesn't require I2C_FUNC_PROTOCOL_MANGLING.
--- drivers/media/i2c/Kconfig | 4 ++++ drivers/media/i2c/Makefile | 1 + drivers/media/i2c/sccb.c | 35 +++++++++++++++++++++++++++++++++++ drivers/media/i2c/sccb.h | 14 ++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 drivers/media/i2c/sccb.c create mode 100644 drivers/media/i2c/sccb.h
drivers/media/i2c/Kconfig +4 -0
@@ -569,6 +569,9 @@ config VIDEO_THS8200 comment "Camera sensor devices" +config SCCB + bool + config VIDEO_APTINA_PLL tristate
@@ -692,6 +695,7 @@ config VIDEO_OV772X tristate "OmniVision OV772x sensor support" depends on I2C && VIDEO_V4L2 depends on MEDIA_CAMERA_SUPPORT + select SCCB ---help--- This is a Video4Linux2 sensor-level driver for the OmniVision OV772x camera.
drivers/media/i2c/Makefile +1 -0
@@ -62,6 +62,7 @@ obj-$(CONFIG_VIDEO_VP27SMPX) += vp27smpx.o obj-$(CONFIG_VIDEO_SONY_BTF_MPX) += sony-btf-mpx.o obj-$(CONFIG_VIDEO_UPD64031A) += upd64031a.o obj-$(CONFIG_VIDEO_UPD64083) += upd64083.o +obj-$(CONFIG_SCCB) += sccb.o obj-$(CONFIG_VIDEO_OV2640) += ov2640.o obj-$(CONFIG_VIDEO_OV2685) += ov2685.o obj-$(CONFIG_VIDEO_OV5640) += ov5640.o
drivers/media/i2c/sccb.c new file +35 -0
@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/i2c.h> + +int sccb_read_byte (struct i2c_client *client,u8 addr) +{ +int ret; +u8 val; + +/* Issue two separated requests in order to avoid repeated start */ + + ret =i2c_master_send (client, &addr,1 ); +if (ret <0 ) +return ret; + + ret =i2c_master_recv (client, &val,1 ); +if (ret <0 ) +return ret; + +return val; +} +EXPORT_SYMBOL_GPL (sccb_read_byte); + +int sccb_write_byte (struct i2c_client *client,u8 addr,u8 data) +{ +int ret; +unsigned char msgbuf[] = { addr, data }; + + ret =i2c_master_send (client, msgbuf,2 ); +if (ret <0 ) +return ret; + +return 0 ; +} +EXPORT_SYMBOL_GPL (sccb_write_byte);
drivers/media/i2c/sccb.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* SCCB helper functions +*/ + +#ifndef __SCCB_H__ +#define __SCCB_H__ + +#include <linux/i2c.h> + +int sccb_read_byte (struct i2c_client *client,u8 addr); +int sccb_write_byte (struct i2c_client *client,u8 addr,u8 data); + +#endif /* __SCCB_H__ */
Signature · 1 line
2.7.4
Hi Mita-san,
Quoted 36 lines · Akinobu Mita
(This patch is in prototype stage)
This adds SCCB helper functions (
sccb_read_byteandsccb_write_byte) that are intended to be used by some of Omnivision sensor drivers.The ov772x driver is going to use these functions in order to make it work with most i2c controllers.
As the ov772x device doesn't support repeated starts, this driver currently requires
I2C_FUNC_PROTOCOL_MANGLINGthat is not supported by many i2c controller drivers.With the
sccb_read_byte()that issues two separated requests in order to avoid repeated start, the driver doesn't requireI2C_FUNC_PROTOCOL_MANGLING.Cc Wolfram Sang <[email protected]>Cc Jacopo Mondi <[email protected]>Cc Laurent Pinchart <[email protected]>Cc Hans Verkuil <[email protected]>Cc Sakari Ailus <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Signed-off-by Akinobu Mita <[email protected]>--- drivers/media/i2c/Kconfig | 4 ++++ drivers/media/i2c/Makefile | 1 + drivers/media/i2c/sccb.c | 35 +++++++++++++++++++++++++++++++++++ drivers/media/i2c/sccb.h | 14 ++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 drivers/media/i2c/sccb.c create mode 100644 drivers/media/i2c/sccb.hdrivers/media/i2c/Kconfig +0
-0
drivers/media/i2c/Kconfig config VIDEO_THS8200 · line 573
Quoted 5 lines
drivers/media/i2c/Kconfig +2
-0@@ -569,6 +569,9 @@ config VIDEO_THS8200 comment "Camera sensor devices" +config SCCB + bool
Tristate? This effectively depends on I²C but I²C is tristate...
Quoted 55 lines
drivers/media/i2c/Kconfig +2
-0+ config VIDEO_APTINA_PLL tristate@@ -692,6 +695,7 @@ config VIDEO_OV772X tristate "OmniVision OV772x sensor support" depends on I2C && VIDEO_V4L2 depends on MEDIA_CAMERA_SUPPORT + select SCCB ---help--- This is a Video4Linux2 sensor-level driver for the OmniVision OV772x camera.drivers/media/i2c/Makefile +1
-0@@ -62,6 +62,7 @@ obj-$(CONFIG_VIDEO_VP27SMPX) += vp27smpx.o obj-$(CONFIG_VIDEO_SONY_BTF_MPX) += sony-btf-mpx.o obj-$(CONFIG_VIDEO_UPD64031A) += upd64031a.o obj-$(CONFIG_VIDEO_UPD64083) += upd64083.o +obj-$(CONFIG_SCCB) += sccb.o obj-$(CONFIG_VIDEO_OV2640) += ov2640.o obj-$(CONFIG_VIDEO_OV2685) += ov2685.o obj-$(CONFIG_VIDEO_OV5640) += ov5640.odrivers/media/i2c/sccb.c new file +25
-0@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/i2c.h> + +int sccb_read_byte (struct i2c_client *client,u8 addr) +{ +int ret; +u8 val; + +/* Issue two separated requests in order to avoid repeated start */ + + ret =i2c_master_send (client, &addr,1 ); +if (ret <0 ) +return ret; + + ret =i2c_master_recv (client, &val,1 ); +if (ret <0 ) +return ret; + +return val; +} +EXPORT_SYMBOL_GPL (sccb_read_byte); + +int sccb_write_byte (struct i2c_client *client,u8 addr,u8 data) +{
drivers/media/i2c/sccb.c line 35
Quoted 10 lines
drivers/media/i2c/sccb.c new file +10
-0+int ret; +unsigned char msgbuf[] = { addr, data }; + + ret =i2c_master_send (client, msgbuf,2 ); +if (ret <0 ) +return ret; + +return 0 ; +} +EXPORT_SYMBOL_GPL (sccb_write_byte);
Please add MODULE_* macros... follows from tristate option.
Quoted 6 lines
drivers/media/i2c/sccb.h new file +0
-0
drivers/media/i2c/sccb.h line 9
Quoted 9 lines
drivers/media/i2c/sccb.h new file +9
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* SCCB helper functions +*/ + +#ifndef __SCCB_H__ +#define __SCCB_H__ + +#include <linux/i2c.h>
struct i2c_client;
is enough; no need to include the whole header here.
Quoted 5 lines
drivers/media/i2c/sccb.h new file +5
-0+ +int sccb_read_byte (struct i2c_client *client,u8 addr); +int sccb_write_byte (struct i2c_client *client,u8 addr,u8 data); + +#endif /* __SCCB_H__ */
Signature · 4 lines
Kind regards, Sakari Ailus [email protected]
Em Fri, 27 Apr 2018 01:13:32 +0900
Akinobu Mita <[email protected]> escreveu:
Quoted 4 lines
(This patch is in prototype stage)
This adds SCCB helper functions (
sccb_read_byteandsccb_write_byte) that are intended to be used by some of Omnivision sensor drivers.
What do you mean by "SCCB"?
Quoted 75 lines
The ov772x driver is going to use these functions in order to make it work with most i2c controllers.
As the ov772x device doesn't support repeated starts, this driver currently requires
I2C_FUNC_PROTOCOL_MANGLINGthat is not supported by many i2c controller drivers.With the
sccb_read_byte()that issues two separated requests in order to avoid repeated start, the driver doesn't requireI2C_FUNC_PROTOCOL_MANGLING.Cc Wolfram Sang <[email protected]>Cc Jacopo Mondi <[email protected]>Cc Laurent Pinchart <[email protected]>Cc Hans Verkuil <[email protected]>Cc Sakari Ailus <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Signed-off-by Akinobu Mita <[email protected]>--- drivers/media/i2c/Kconfig | 4 ++++ drivers/media/i2c/Makefile | 1 + drivers/media/i2c/sccb.c | 35 +++++++++++++++++++++++++++++++++++ drivers/media/i2c/sccb.h | 14 ++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 drivers/media/i2c/sccb.c create mode 100644 drivers/media/i2c/sccb.hdrivers/media/i2c/Kconfig +4
-0@@ -569,6 +569,9 @@ config VIDEO_THS8200 comment "Camera sensor devices" +config SCCB + bool + config VIDEO_APTINA_PLL tristate@@ -692,6 +695,7 @@ config VIDEO_OV772X tristate "OmniVision OV772x sensor support" depends on I2C && VIDEO_V4L2 depends on MEDIA_CAMERA_SUPPORT + select SCCB ---help--- This is a Video4Linux2 sensor-level driver for the OmniVision OV772x camera.drivers/media/i2c/Makefile +1
-0@@ -62,6 +62,7 @@ obj-$(CONFIG_VIDEO_VP27SMPX) += vp27smpx.o obj-$(CONFIG_VIDEO_SONY_BTF_MPX) += sony-btf-mpx.o obj-$(CONFIG_VIDEO_UPD64031A) += upd64031a.o obj-$(CONFIG_VIDEO_UPD64083) += upd64083.o +obj-$(CONFIG_SCCB) += sccb.o obj-$(CONFIG_VIDEO_OV2640) += ov2640.o obj-$(CONFIG_VIDEO_OV2685) += ov2685.o obj-$(CONFIG_VIDEO_OV5640) += ov5640.odrivers/media/i2c/sccb.c new file +8
-0@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/i2c.h> + +int sccb_read_byte (struct i2c_client *client,u8 addr) +{ +int ret; +u8 val;
drivers/media/i2c/sccb.c line 18
Quoted 10 lines
drivers/media/i2c/sccb.c new file +10
-0+ +/* Issue two separated requests in order to avoid repeated start */ + + ret =i2c_master_send (client, &addr,1 ); +if (ret <0 ) +return ret; + + ret =i2c_master_recv (client, &val,1 ); +if (ret <0 ) +return ret;
Handling it this way is a very bad idea, as you may have an operation between those two, as you're locking/unlocking for each i2c_master call, e. g. the code should be, instead:
i2c_lock_adapter(); __i2c_transfer(); /* Send */ __i2c_transfer(); /* Receive */ i2c_unlock_adapter();
Also, if the problem is just due to I2C not supporting REPEAT, IMHO, the best would be to add some IRC flag to indicate that.
Btw, this is not the first device that doesn't support repeats. A good hint of drivers with similar issues is:
$ git grep i2c_lock_adapter drivers/media/ drivers/media/dvb-frontends/af9013.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/af9013.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/drxk_hard.c: i2c_lock_adapter(state->i2c); drivers/media/dvb-frontends/rtl2830.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/rtl2830.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/rtl2830.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/tda1004x.c: i2c_lock_adapter(state->i2c); drivers/media/tuners/tda18271-common.c: i2c_lock_adapter(priv->i2c_props.adap); drivers/media/tuners/tda18271-common.c: i2c_lock_adapter(priv->i2c_props.adap);
Regards,
Mauro
Hi,
Quoted 51 lines · Akinobu Mita
drivers/media/i2c/sccb.c new file +35
-0@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/i2c.h> + +int sccb_read_byte (struct i2c_client *client,u8 addr) +{ +int ret; +u8 val; + +/* Issue two separated requests in order to avoid repeated start */ + + ret =i2c_master_send (client, &addr,1 ); +if (ret <0 ) +return ret; + + ret =i2c_master_recv (client, &val,1 ); +if (ret <0 ) +return ret; + +return val; +} +EXPORT_SYMBOL_GPL (sccb_read_byte); + +int sccb_write_byte (struct i2c_client *client,u8 addr,u8 data) +{ +int ret; +unsigned char msgbuf[] = { addr, data }; + + ret =i2c_master_send (client, msgbuf,2 ); +if (ret <0 ) +return ret; + +return 0 ; +} +EXPORT_SYMBOL_GPL (sccb_write_byte);drivers/media/i2c/sccb.h new file +4
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* SCCB helper functions +*/
drivers/media/i2c/sccb.h line 14
Quoted 10 lines
drivers/media/i2c/sccb.h new file +10
-0+ +#ifndef __SCCB_H__ +#define __SCCB_H__ + +#include <linux/i2c.h> + +int sccb_read_byte (struct i2c_client *client,u8 addr); +int sccb_write_byte (struct i2c_client *client,u8 addr,u8 data); + +#endif /* __SCCB_H__ */
The functions look very simple. Have you considered moving them into sccb.h as static inline?
-- Sebastian
Quoted 59 lines · Sebastian Reichel
Hi,
Quoted 56 lines · Akinobu Mita
drivers/media/i2c/sccb.c new file +35
-0@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/i2c.h> + +int sccb_read_byte (struct i2c_client *client,u8 addr) +{ +int ret; +u8 val; + +/* Issue two separated requests in order to avoid repeated start */ + + ret =i2c_master_send (client, &addr,1 ); +if (ret <0 ) +return ret; + + ret =i2c_master_recv (client, &val,1 ); +if (ret <0 ) +return ret; + +return val; +} +EXPORT_SYMBOL_GPL (sccb_read_byte); + +int sccb_write_byte (struct i2c_client *client,u8 addr,u8 data) +{ +int ret; +unsigned char msgbuf[] = { addr, data }; + + ret =i2c_master_send (client, msgbuf,2 ); +if (ret <0 ) +return ret; + +return 0 ; +} +EXPORT_SYMBOL_GPL (sccb_write_byte);drivers/media/i2c/sccb.h new file +9
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* SCCB helper functions +*/ + +#ifndef __SCCB_H__ +#define __SCCB_H__ + +#include <linux/i2c.h>
Quoted 8 lines
drivers/media/i2c/sccb.h line 14
Quoted 5 lines
drivers/media/i2c/sccb.h new file +5
-0+ +int sccb_read_byte (struct i2c_client *client,u8 addr); +int sccb_write_byte (struct i2c_client *client,u8 addr,u8 data); + +#endif /* __SCCB_H__ */ The functions look very simple. Have you considered moving them into
sccb.has static inline?
I agree. (Considering my previous comment on the dependencies, this is a better idea. No need for a module this small.)
Signature · 2 lines
Sakari Ailus e-mail: [email protected]
2018-05-05 23:51 GMT+09:00 Mauro Carvalho Chehab <[email protected]>:
Quoted 9 lines
Em Fri, 27 Apr 2018 01:13:32 +0900
Akinobu Mita <[email protected]> escreveu:Quoted 4 lines
(This patch is in prototype stage)
This adds SCCB helper functions (
sccb_read_byteandsccb_write_byte) that are intended to be used by some of Omnivision sensor drivers.What do you mean by "SCCB"?
Serial Camera Control Bus (SCCB). I'll write SCCB and the non abbreviation together in the comment block and commit log.
Quoted 90 lines
Quoted 75 lines
The ov772x driver is going to use these functions in order to make it work with most i2c controllers.
As the ov772x device doesn't support repeated starts, this driver currently requires
I2C_FUNC_PROTOCOL_MANGLINGthat is not supported by many i2c controller drivers.With the
sccb_read_byte()that issues two separated requests in order to avoid repeated start, the driver doesn't requireI2C_FUNC_PROTOCOL_MANGLING.Cc Wolfram Sang <[email protected]>Cc Jacopo Mondi <[email protected]>Cc Laurent Pinchart <[email protected]>Cc Hans Verkuil <[email protected]>Cc Sakari Ailus <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Signed-off-by Akinobu Mita <[email protected]>--- drivers/media/i2c/Kconfig | 4 ++++ drivers/media/i2c/Makefile | 1 + drivers/media/i2c/sccb.c | 35 +++++++++++++++++++++++++++++++++++ drivers/media/i2c/sccb.h | 14 ++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 drivers/media/i2c/sccb.c create mode 100644 drivers/media/i2c/sccb.hdrivers/media/i2c/Kconfig +4
-0@@ -569,6 +569,9 @@ config VIDEO_THS8200 comment "Camera sensor devices" +config SCCB + bool + config VIDEO_APTINA_PLL tristate@@ -692,6 +695,7 @@ config VIDEO_OV772X tristate "OmniVision OV772x sensor support" depends on I2C && VIDEO_V4L2 depends on MEDIA_CAMERA_SUPPORT + select SCCB ---help--- This is a Video4Linux2 sensor-level driver for the OmniVision OV772x camera.drivers/media/i2c/Makefile +1
-0@@ -62,6 +62,7 @@ obj-$(CONFIG_VIDEO_VP27SMPX) += vp27smpx.o obj-$(CONFIG_VIDEO_SONY_BTF_MPX) += sony-btf-mpx.o obj-$(CONFIG_VIDEO_UPD64031A) += upd64031a.o obj-$(CONFIG_VIDEO_UPD64083) += upd64083.o +obj-$(CONFIG_SCCB) += sccb.o obj-$(CONFIG_VIDEO_OV2640) += ov2640.o obj-$(CONFIG_VIDEO_OV2685) += ov2685.o obj-$(CONFIG_VIDEO_OV5640) += ov5640.odrivers/media/i2c/sccb.c new file +8
-0@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/i2c.h> + +int sccb_read_byte (struct i2c_client *client,u8 addr) +{ +int ret; +u8 val;drivers/media/i2c/sccb.c line 18
Quoted 10 lines
drivers/media/i2c/sccb.c new file +10
-0+ +/* Issue two separated requests in order to avoid repeated start */ + + ret =i2c_master_send (client, &addr,1 ); +if (ret <0 ) +return ret; + + ret =i2c_master_recv (client, &val,1 ); +if (ret <0 ) +return ret;Handling it this way is a very bad idea, as you may have an operation between those two, as you're locking/unlocking for each
i2c_mastercall, e. g. the code should be, instead:
Quoted 7 lines
i2c_lock_adapter(); __i2c_transfer(); /* Send */ __i2c_transfer(); /* Receive */ i2c_unlock_adapter();Also, if the problem is just due to I2C not supporting REPEAT, IMHO, the best would be to add some IRC flag to indicate that.
I sent a patch using this approach.
Quoted 16 lines
Btw, this is not the first device that doesn't support repeats. A good hint of drivers with similar issues is:
$ git grep i2c_lock_adapter drivers/media/ drivers/media/dvb-frontends/af9013.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/af9013.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/drxk_hard.c: i2c_lock_adapter(state->i2c); drivers/media/dvb-frontends/rtl2830.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/rtl2830.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/rtl2830.c: i2c_lock_adapter(client->adapter); drivers/media/dvb-frontends/tda1004x.c: i2c_lock_adapter(state->i2c); drivers/media/tuners/tda18271-common.c: i2c_lock_adapter(priv->i2c_props.adap); drivers/media/tuners/tda18271-common.c: i2c_lock_adapter(priv->i2c_props.adap);Regards,
Mauro