linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] LEGO MINDSTORMS I2C support
@ 2016-09-05 20:40 David Lechner
  2016-09-05 20:40 ` [PATCH 1/3] i2c: Add class for LEGO MINDSTORMS sensors David Lechner
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: David Lechner @ 2016-09-05 20:40 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: David Lechner, linux-i2c, linux-kernel

I'm working on getting LEGO MINDSTORMS[1] support in the Linux kernel.

They have a system of modular sensors that are hot-plugable, some of which use
I2C communications. Unfortunately, these don't necessary follow standard I2C
conventions, but they do have a well-defined register layout, so they are
easy to detect.

This set of patches addresses the hot-plugability of the sensors.

[1]: http://mindstorms.lego.com


David Lechner (3):
  i2c: Add class for LEGO MINDSTORMS sensors
  i2c: Add special case for detecting LEGO devices
  i2c: expose adapter probe and remove probed functions

 drivers/i2c/i2c-core.c | 62 ++++++++++++++++++++++++++++++++++++++------------
 include/linux/i2c.h    |  4 ++++
 2 files changed, 52 insertions(+), 14 deletions(-)

-- 
2.7.4

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/3] i2c: Add class for LEGO MINDSTORMS sensors
  2016-09-05 20:40 [PATCH 0/3] LEGO MINDSTORMS I2C support David Lechner
@ 2016-09-05 20:40 ` David Lechner
  2016-09-06 19:01   ` Uwe Kleine-König
  2016-09-05 20:40 ` [PATCH 2/3] i2c: Add special case for detecting LEGO devices David Lechner
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: David Lechner @ 2016-09-05 20:40 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: David Lechner, linux-i2c, linux-kernel

LEGO MINDSTORMS (robotics system from LEGO) has a number of sensors
that use I2C communications. These sensors have a well-know register
layout, so they are easily detected.

This class is being added so that future drivers for these sensors can
be automatically detected.

Signed-off-by: David Lechner <david@lechnology.com>
---
 include/linux/i2c.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index fffdc27..3eab858 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -638,6 +638,7 @@ i2c_unlock_adapter(struct i2c_adapter *adapter)
 #define I2C_CLASS_DDC		(1<<3)	/* DDC bus on graphics adapters */
 #define I2C_CLASS_SPD		(1<<7)	/* Memory modules */
 #define I2C_CLASS_DEPRECATED	(1<<8)	/* Warn users that adapter will stop using classes */
+#define I2C_CLASS_LEGO		(1<<9)	/* LEGO MINDSTORMS sensors */
 
 /* Internal numbers to terminate lists */
 #define I2C_CLIENT_END		0xfffeU
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/3] i2c: Add special case for detecting LEGO devices
  2016-09-05 20:40 [PATCH 0/3] LEGO MINDSTORMS I2C support David Lechner
  2016-09-05 20:40 ` [PATCH 1/3] i2c: Add class for LEGO MINDSTORMS sensors David Lechner
@ 2016-09-05 20:40 ` David Lechner
  2016-09-05 20:40 ` [PATCH 3/3] i2c: expose adapter probe and remove probed functions David Lechner
  2016-09-15 20:00 ` [PATCH 0/3] LEGO MINDSTORMS I2C support Wolfram Sang
  3 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2016-09-05 20:40 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: David Lechner, linux-i2c, linux-kernel

LEGO chose to ignore the I2C specification and has created devices with
I2C addresses of 0x01 and 0x02. i2c_check_7bit_addr_validity_strict()
disallows these addresses, so we need a special case to skip this for
LEGO sensors.

Furthermore, LEGO devices do not respond to i2c_default_probe(), so we
skip this as a special case as well.

Signed-off-by: David Lechner <david@lechnology.com>
---
 drivers/i2c/i2c-core.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index da3a02e..28436d9 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -2591,12 +2591,14 @@ static int i2c_detect_address(struct i2c_client *temp_client,
 	int addr = temp_client->addr;
 	int err;
 
-	/* Make sure the address is valid */
-	err = i2c_check_7bit_addr_validity_strict(addr);
-	if (err) {
-		dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
-			 addr);
-		return err;
+	/* Make sure the address is valid - LEGO devices break the rules */
+	if (!(driver->class & I2C_CLASS_LEGO)) {
+		err = i2c_check_7bit_addr_validity_strict(addr);
+		if (err) {
+			dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
+				 addr);
+			return err;
+		}
 	}
 
 	/* Skip if already in use (7 bit, no need to encode flags) */
@@ -2604,7 +2606,8 @@ static int i2c_detect_address(struct i2c_client *temp_client,
 		return 0;
 
 	/* Make sure there is something at this address */
-	if (!i2c_default_probe(adapter, addr))
+	if (!(driver->class & I2C_CLASS_LEGO) &&
+	    !i2c_default_probe(adapter, addr))
 		return 0;
 
 	/* Finally call the custom detection function */
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/3] i2c: expose adapter probe and remove probed functions
  2016-09-05 20:40 [PATCH 0/3] LEGO MINDSTORMS I2C support David Lechner
  2016-09-05 20:40 ` [PATCH 1/3] i2c: Add class for LEGO MINDSTORMS sensors David Lechner
  2016-09-05 20:40 ` [PATCH 2/3] i2c: Add special case for detecting LEGO devices David Lechner
@ 2016-09-05 20:40 ` David Lechner
  2016-09-16 21:03   ` Wolfram Sang
  2016-09-15 20:00 ` [PATCH 0/3] LEGO MINDSTORMS I2C support Wolfram Sang
  3 siblings, 1 reply; 9+ messages in thread
From: David Lechner @ 2016-09-05 20:40 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: David Lechner, linux-i2c, linux-kernel

Publicly expose functions for detecting devices and removing detected
devices.

Currently an i2c adapter only probes/detects new devices when the adapter
is added or when a new i2c driver is added. This adds a new function,
i2c_adapter_probe(), that allows detecting sensors at any time.

Furthermore, the function i2c_adapter_remove_probed() is added to remove
the devices added by i2c_adapter_probe() at any time.

The intended use of these functions is for LEGO MINDSTORMS sensors. These
are hot-plugable I2C devices. When a sensor is connected, i2c_adapter_probe()
is called to automatically configure the sensor. When the sensor is
disconnected, i2c_adapter_remove_probed() is called to remove the
automatically configured device.

Signed-off-by: David Lechner <david@lechnology.com>
---
 drivers/i2c/i2c-core.c | 45 ++++++++++++++++++++++++++++++++++++++-------
 include/linux/i2c.h    |  3 +++
 2 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 28436d9..2781a88 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1691,6 +1691,23 @@ static int __process_new_adapter(struct device_driver *d, void *data)
 	return i2c_do_add_adapter(to_i2c_driver(d), data);
 }
 
+/**
+ * i2c_adapter_probe - probe adapter for clients
+ * @adap: The i2c adapter.
+ *
+ * This calls the detect function of each driver that matches the class of the
+ * adapter. This function is called when an adapter is added, so there is no
+ * need to call this manually, unless you have hot-plugable i2c devices that
+ * are connected after the adapter is created.
+ */
+void i2c_adapter_probe(struct i2c_adapter *adap)
+{
+	mutex_lock(&core_lock);
+	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
+	mutex_unlock(&core_lock);
+}
+EXPORT_SYMBOL(i2c_adapter_probe);
+
 static int i2c_register_adapter(struct i2c_adapter *adap)
 {
 	int res = -EINVAL;
@@ -1759,9 +1776,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 		i2c_scan_static_board_info(adap);
 
 	/* Notify drivers */
-	mutex_lock(&core_lock);
-	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
-	mutex_unlock(&core_lock);
+	i2c_adapter_probe(adap);
 
 	return 0;
 
@@ -1904,6 +1919,25 @@ static int __process_removed_adapter(struct device_driver *d, void *data)
 }
 
 /**
+ * i2c_adapter_remove_probed - Remove clients that were automatically detected.
+ * @adap: The i2c adapter.
+ *
+ * This removes all i2c clients from this adapter that were added via driver
+ * detect() functions. This function is called when an adapter is removed, so
+ * there is no need to call this manually, unless you have hot-plugable i2c
+ * devices that are disconnected before the adapter is destroyed.
+ *
+ * Any clients that were added manually (e.g. via sysfs) are not removed.
+ */
+void i2c_adapter_remove_probed(struct i2c_adapter *adap)
+{
+	mutex_lock(&core_lock);
+	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_removed_adapter);
+	mutex_unlock(&core_lock);
+}
+EXPORT_SYMBOL(i2c_adapter_remove_probed);
+
+/**
  * i2c_del_adapter - unregister I2C adapter
  * @adap: the adapter being unregistered
  * Context: can sleep
@@ -1927,10 +1961,7 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 
 	acpi_i2c_remove_space_handler(adap);
 	/* Tell drivers about this removal */
-	mutex_lock(&core_lock);
-	bus_for_each_drv(&i2c_bus_type, NULL, adap,
-			       __process_removed_adapter);
-	mutex_unlock(&core_lock);
+	i2c_adapter_remove_probed(adap);
 
 	/* Remove devices instantiated from sysfs */
 	mutex_lock_nested(&adap->userspace_clients_lock,
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 3eab858..0016623 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -657,6 +657,9 @@ extern int i2c_add_adapter(struct i2c_adapter *);
 extern void i2c_del_adapter(struct i2c_adapter *);
 extern int i2c_add_numbered_adapter(struct i2c_adapter *);
 
+extern void i2c_adapter_probe(struct i2c_adapter *adap);
+extern void i2c_adapter_remove_probed(struct i2c_adapter *adap);
+
 extern int i2c_register_driver(struct module *, struct i2c_driver *);
 extern void i2c_del_driver(struct i2c_driver *);
 
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/3] i2c: Add class for LEGO MINDSTORMS sensors
  2016-09-05 20:40 ` [PATCH 1/3] i2c: Add class for LEGO MINDSTORMS sensors David Lechner
@ 2016-09-06 19:01   ` Uwe Kleine-König
  0 siblings, 0 replies; 9+ messages in thread
From: Uwe Kleine-König @ 2016-09-06 19:01 UTC (permalink / raw)
  To: David Lechner; +Cc: Wolfram Sang, linux-i2c, linux-kernel

Hello David,

On Mon, Sep 05, 2016 at 03:40:12PM -0500, David Lechner wrote:
> that use I2C communications. These sensors have a well-know register
s/w/wn/

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/3] LEGO MINDSTORMS I2C support
  2016-09-05 20:40 [PATCH 0/3] LEGO MINDSTORMS I2C support David Lechner
                   ` (2 preceding siblings ...)
  2016-09-05 20:40 ` [PATCH 3/3] i2c: expose adapter probe and remove probed functions David Lechner
@ 2016-09-15 20:00 ` Wolfram Sang
  2016-09-16 20:05   ` David Lechner
  3 siblings, 1 reply; 9+ messages in thread
From: Wolfram Sang @ 2016-09-15 20:00 UTC (permalink / raw)
  To: David Lechner; +Cc: linux-i2c, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 969 bytes --]

On Mon, Sep 05, 2016 at 03:40:11PM -0500, David Lechner wrote:
> I'm working on getting LEGO MINDSTORMS[1] support in the Linux kernel.
> 
> They have a system of modular sensors that are hot-plugable, some of which use
> I2C communications. Unfortunately, these don't necessary follow standard I2C
> conventions, but they do have a well-defined register layout, so they are
> easy to detect.
> 
> This set of patches addresses the hot-plugability of the sensors.
> 
> [1]: http://mindstorms.lego.com

Thanks!

For a review, I'd need users of this functionality. In this case, a
master driver and probably a sensor driver. Can any regular I2C master
talk to these devices if they support talking to addresses 0x00-0x7f? My
first impression is that using a class is not the proper approach
because classes were used to limit access to devices, not to extend.
However, I'd need to see more code to check if that holds true.

Thanks,

   Wolfram


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/3] LEGO MINDSTORMS I2C support
  2016-09-15 20:00 ` [PATCH 0/3] LEGO MINDSTORMS I2C support Wolfram Sang
@ 2016-09-16 20:05   ` David Lechner
  0 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2016-09-16 20:05 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c, linux-kernel

On 09/15/2016 03:00 PM, Wolfram Sang wrote:
> On Mon, Sep 05, 2016 at 03:40:11PM -0500, David Lechner wrote:
>> I'm working on getting LEGO MINDSTORMS[1] support in the Linux kernel.
>>
>> They have a system of modular sensors that are hot-plugable, some of which use
>> I2C communications. Unfortunately, these don't necessary follow standard I2C
>> conventions, but they do have a well-defined register layout, so they are
>> easy to detect.
>>
>> This set of patches addresses the hot-plugability of the sensors.
>>
>> [1]: http://mindstorms.lego.com
>
> Thanks!
>
> For a review, I'd need users of this functionality. In this case, a
> master driver and probably a sensor driver.

Work in progress drivers:

master: 
https://github.com/ev3dev/lego-linux-drivers/blob/master/ev3/legoev3_i2c.c
master: 
https://github.com/ev3dev/lego-linux-drivers/blob/master/evb/evb_pru_i2c.c
sensor: 
https://github.com/ev3dev/lego-linux-drivers/blob/master/sensors/nxt_i2c_sensor_core.c

> Can any regular I2C master
> talk to these devices if they support talking to addresses 0x00-0x7f?

Yes. I've tested this with the SoC I2C on Raspberry Pi. There are 
several 3rd party LEGO MINDSTORMS compatible addon boards for RPi that 
use the SoC I2C with LEGO sensors, which is part of my motivation for 
this patchset.

> My first impression is that using a class is not the proper approach
> because classes were used to limit access to devices, not to extend.
> However, I'd need to see more code to check if that holds true.
>

I'm certainly open to other suggestions. This just seemed like the 
obvious way to me.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/3] i2c: expose adapter probe and remove probed functions
  2016-09-05 20:40 ` [PATCH 3/3] i2c: expose adapter probe and remove probed functions David Lechner
@ 2016-09-16 21:03   ` Wolfram Sang
  2016-09-16 21:09     ` David Lechner
  0 siblings, 1 reply; 9+ messages in thread
From: Wolfram Sang @ 2016-09-16 21:03 UTC (permalink / raw)
  To: David Lechner; +Cc: linux-i2c, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 371 bytes --]


> The intended use of these functions is for LEGO MINDSTORMS sensors. These
> are hot-plugable I2C devices. When a sensor is connected, i2c_adapter_probe()
> is called to automatically configure the sensor. When the sensor is
> disconnected, i2c_adapter_remove_probed() is called to remove the
> automatically configured device.

How do you detect the hot-plug events?


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/3] i2c: expose adapter probe and remove probed functions
  2016-09-16 21:03   ` Wolfram Sang
@ 2016-09-16 21:09     ` David Lechner
  0 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2016-09-16 21:09 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c, linux-kernel

On 09/16/2016 04:03 PM, Wolfram Sang wrote:
>
>> The intended use of these functions is for LEGO MINDSTORMS sensors. These
>> are hot-plugable I2C devices. When a sensor is connected, i2c_adapter_probe()
>> is called to automatically configure the sensor. When the sensor is
>> disconnected, i2c_adapter_remove_probed() is called to remove the
>> automatically configured device.
>
> How do you detect the hot-plug events?
>

There are "port" drivers that do this. An I2C adapter is assigned to 
each port via device-tree or platform data struct.

Port driver code if you are interested:
- 
https://github.com/ev3dev/lego-linux-drivers/blob/master/ev3/legoev3_ports_in.c
- 
https://github.com/ev3dev/lego-linux-drivers/blob/master/evb/evb_ports_in.c

In particular, here is where we call the new i2c_adapter_probe() 
function: 
https://github.com/ev3dev/lego-linux-drivers/blob/master/evb/evb_ports_in.c#L543

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-09-16 21:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-05 20:40 [PATCH 0/3] LEGO MINDSTORMS I2C support David Lechner
2016-09-05 20:40 ` [PATCH 1/3] i2c: Add class for LEGO MINDSTORMS sensors David Lechner
2016-09-06 19:01   ` Uwe Kleine-König
2016-09-05 20:40 ` [PATCH 2/3] i2c: Add special case for detecting LEGO devices David Lechner
2016-09-05 20:40 ` [PATCH 3/3] i2c: expose adapter probe and remove probed functions David Lechner
2016-09-16 21:03   ` Wolfram Sang
2016-09-16 21:09     ` David Lechner
2016-09-15 20:00 ` [PATCH 0/3] LEGO MINDSTORMS I2C support Wolfram Sang
2016-09-16 20:05   ` David Lechner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).