Skip to contentSkip to conversation

lis3lv02d: Add STMicroelectronics lis331dlh digital accelerometer

PATCH In review AnilKumar Ch 4 participants 14 messages Aug 22, 2012 – Sep 24, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lis3lv02d: Add STMicroelectronics lis331dlh digital accelerometer

This patch adds support for lis331dlh digital accelerometer to the lis3lv02d driver family. Adds ID field for detecting the lis331dlh module, based on this ID field lis3lv02d driver will export the lis331dlh module functionality.

---
Changes from v1:
- Removed G range sysfs entry from v1
- Modified documentation to specify lis331dlh is supported

 Documentation/misc-devices/lis3lv02d   |    3 ++-
 drivers/misc/lis3lv02d/lis3lv02d.c     |   42 +++++++++++++++++++++++++++++-
 drivers/misc/lis3lv02d/lis3lv02d.h     |   44 +++++++++++++++++++++++++++-----
 drivers/misc/lis3lv02d/lis3lv02d_i2c.c |    7 ++++-
 4 files changed, 87 insertions(+), 9 deletions(-)
Documentation/misc-devices/lis3lv02d +2 -1
index f1a4ec8..af815b9 100644
@@ -4,7 +4,8 @@ Kernel driver lis3lv02d
 Supported chips:
 
   * STMicroelectronics LIS3LV02DL, LIS3LV02DQ (12 bits precision)
-  * STMicroelectronics LIS302DL, LIS3L02DQ, LIS331DL (8 bits)
+  * STMicroelectronics LIS302DL, LIS3L02DQ, LIS331DL (8 bits) and
+    LIS331DLH (16 bits)
 
 Authors:
         Yan Burman <[email protected]>
drivers/misc/lis3lv02d/lis3lv02d.c +41 -1

81 lines not on this page · raw message

drivers/misc/lis3lv02d/lis3lv02d.h +38 -6

74 lines not on this page · raw message

drivers/misc/lis3lv02d/lis3lv02d_i2c.c +6 -1
index c02fea0..98cf9ba 100644
@@ -90,7 +90,11 @@ static int lis3_i2c_init(struct lis3lv02d *lis3)
 	if (ret < 0)
 		return ret;
 
-	reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+	if (lis3->whoami == WAI_3DLH)
+		reg |= CTRL1_PM0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+	else
+		reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+
 	return lis3->write(lis3, CTRL_REG1, reg);
 }
 
@@ -232,6 +236,7 @@ static int lis3_i2c_runtime_resume(struct device *dev)
 
 static const struct i2c_device_id lis3lv02d_id[] = {
 	{"lis3lv02d", 0 },
+	{"lis331dlh", LIS331DLH},
 	{}
 };
 
Signature · 1 line
1.7.9.5
#2 lore raw

Hi All,

A few nitpicks.

Quoted 3 lines

+ * LIS3331DLH spec says 1LSBs corresponds 4G/1024 -> 1LSB is 1000/1024 mG.
+ * Sensitivity values for +/-2G, outdata in 12 bits for +/-2G scale. so 4
+ * bits adjustment is required

Shouldn't it be "1LSB is 4000/1024 mG" ?
Also "outdata in 12bits" (typo. in->is?)

On a more technical note, now that LIS3331DLH has 16bit resolution, why don't we simply return the entire 16-bit value in lis3lv02d_read_16(). The fact that lis3lv02d_read_16() has 16-bit resolution can be indicated by

@@ -954,6 +984,16 @@ int lis3lv02d_init_device(struct lis3lv02d *lis3)

                lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
                lis3->scale = LIS3_SENSITIVITY_8B;
                break;
+       case WAI_3DLH:
+               pr_info("16 bits 3DLH sensor found\n");
+               lis3->read_data = lis3lv02d_read_16;
+               lis3->mdps_max_val = 32768; /* 16 bits for +/-2G */
Signature · 2 lines
regards
CVS
#3 lore raw

Hi Chinmay,

Thanks for the comments

Quoted 8 lines · Chinmay V S

Hi All,

A few nitpicks.

Quoted 3 lines

+ * LIS3331DLH spec says 1LSBs corresponds 4G/1024 -> 1LSB is 1000/1024 mG.
+ * Sensitivity values for +/-2G, outdata in 12 bits for +/-2G scale. so 4
+ * bits adjustment is required

Shouldn't it be "1LSB is 4000/1024 mG" ?

Typo mistake this should be 4G/4096

Quoted 2 lines

Also "outdata in 12bits" (typo. in->is?)

If we look at the lis331dlh datasheet 12 bits outdata for +/- 2G resolution range and 13 bits for +/- 4G range and 14 bits for +/- 8G range. So output data is only 12 bits.

Quoted 4 lines

On a more technical note, now that LIS3331DLH has 16bit resolution, why don't we simply return the entire 16-bit value in lis3lv02d_read_16(). The fact that lis3lv02d_read_16() has 16-bit resolution can be indicated by

Depending on the range 2 or 4 or 8 we have to pass the outdata that is handled by using the shift_adj member to shift the outdata to corresponding bits 12/13/14.

drivers/misc/lis3lv02d/lis3lv02d.c int lis3lv02d_init_device(struct lis3lv02d *lis3) · line 990

Quoted 9 lines
drivers/misc/lis3lv02d/lis3lv02d.c +4 -0
@@ -954,6 +984,16 @@ int lis3lv02d_init_device(struct lis3lv02d *lis3)
                lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
                lis3->scale = LIS3_SENSITIVITY_8B;
                break;
+       case WAI_3DLH:
+               pr_info("16 bits 3DLH sensor found\n");
+               lis3->read_data = lis3lv02d_read_16;
+               lis3->mdps_max_val = 32768; /* 16 bits for +/-2G */

This driver supports only +/-2G range of values so it is limited to 2048. With this we do not have the runtime change of G range so by default 2G is added.

Regards
AnilKumar

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#4 lore raw

Hmmm. Interesting. As i understand LIS331DLH provides 16bit data irrespective of the full-scale/sensitivity configuration. Hence we could effectively map +/-2G to +/-32768(signed 16bit 2's complement). According to the current-patch right-shifting the register values by 4(i.e. reducing 16bit --> 12bit) will mean that we lose accuracy by ~1mG.

Clearly this will NOT affect use-case like display-orientation in smart-phones, but surely medical and industrial applications WILL benefit from the additional accuracy by utilising the entire 16-bit resolution provided by LIS331DLH hardware.

I went through the LIS331DLH datasheet/application-note from
http://www.st.com/internet/analog/product/218132.jsp and i'm a bit confused from your statement about +/-2G being 12bit data. Nowhere is it mentioned that LIS331DLH provides +/-2G|+/-4G|+/-8G as 12|13|14 bit data respectively. Then again i might be wrong...

Signature · 2 lines
regards
ChinmayVS
#5 lore raw
Quoted 12 lines · Chinmay V S

Hmmm. Interesting. As i understand LIS331DLH provides 16bit data irrespective of the full-scale/sensitivity configuration. Hence we could effectively map +/-2G to +/-32768(signed 16bit 2's complement). According to the current-patch right-shifting the register values by 4(i.e. reducing 16bit --> 12bit) will mean that we lose accuracy by ~1mG.

Clearly this will NOT affect use-case like display-orientation in smart-phones, but surely medical and industrial applications WILL benefit from the additional accuracy by utilising the entire 16-bit resolution provided by LIS331DLH hardware.

Quoted 6 lines

I went through the LIS331DLH datasheet/application-note from
http://www.st.com/internet/analog/product/218132.jsp and i'm a bit confused from your statement about +/-2G being 12bit data. Nowhere is it mentioned that LIS331DLH provides +/-2G|+/-4G|+/-8G as 12|13|14 bit data respectively. Then again i might be wrong...

Look at this application note which talks about the outdata values
for 2G range (page 12/31) http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/APPLICATION_NOTE/CD00215823.pdf

Corresponding to the 4G and 8G I got the details form older patches (SHIFT_ADJ_4G and SHIFT_ADJ_8G).
http://driverdev.linuxdriverproject.org/pipermail/devel/2010-November/009685.html

We can easily interpret number of bits for 4G and 8G from 2G information.

Thanks
AnilKumar

#6 lore raw
Quoted 2 lines

Look at this application note which talks about the outdata values
for 2G range (page 12/31) http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/APPLICATION_NOTE/CD00215823.pdf

Had been through the application note earlier. The table5 (on page 12) that you refer to, does NOT contradict either 12/16bit, as in all the examples the lower 4 bits are zero. So i don't see how one can assume from these examples that for +/-2G they one should consider 12/16bits. A nice side-effect of using 12|13|14bits for +/-2|4|8G is that the values returned by the driver are in mG in all the 3 modes.

Quoted 6 lines

Corresponding to the 4G and 8G I got the details form older patches (SHIFT_ADJ_4G and SHIFT_ADJ_8G).
http://driverdev.linuxdriverproject.org/pipermail/devel/2010-November/009685.html

We can easily interpret number of bits for 4G and 8G from 2G information.

Going through the code of this driver i can see what you are talking about. Depending on the full-scale-range the device is being configured for, the number of bits used to represent acceleration in the driver is changed.

Again judging from the code, the driver is always returning acceleration at a constant accuracy i.e. 1mG in all the 3 modes (+/-2|4|8G)i.e.
+/-2G is mode means value can be anywhere from +/-2048mG, (requiring 12bits.)
+/-4G in the range of +/-4096mG, requiring 13bits. +/-8G i.e. +/-8192mG, requiring 14bits.

Was this done...

a. ...because LIS331DLH's theoretical MAX accuracy is ~1mG If yes, then using 12bits is fine.

-OR-

b. ...so that the driver will report values at a constant scale(i.e.mG) regardless of the mode?
If yes, then maybe we could consider using the additional bits to obtain the maximum possible accuracy the LIS331DLH supports rather than choosing to discard the LSBits.

(Can anyone please confirm the above?)

Going through the datasheet of LIS331DLH,
http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00213470.pdf
we see that accuracy/sensitivity of the device as per Table 2.1 (page9/38) is 1|2|4mG in +/-2|4|8Gmodes respectively. It is mentioned in the footnotes that the readings were taken at 12bit resolution(a point in favour of using 12bits ourselves). But its is still NOT clear whether the accuracy/sensitivity are the LIS331DLH's physical limits or whether they stem from the fact that ONLY 12bits were used during the rating tests.

I hope i'm not being too much of a PIA. My sole intention is to see that we do NOT unintentionally lose track of the device capabilities in the midst of all these driver iterations. Thank you for your patience.

PS: One more nitpick

Quoted 1 line

LIS3331DLH spec says 1LSBs(...)

LIS3331DLH --> LIS331DLH

Signature · 2 lines
regards
ChinmayVS
#7 lore raw
Quoted 30 lines · Chinmay V S
Quoted 2 lines

Look at this application note which talks about the outdata values
for 2G range (page 12/31) http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/APPLICATION_NOTE/CD00215823.pdf

Had been through the application note earlier. The table5 (on page 12) that you refer to, does NOT contradict either 12/16bit, as in all the examples the lower 4 bits are zero. So i don't see how one can assume from these examples that for +/-2G they one should consider 12/16bits. A nice side-effect of using 12|13|14bits for +/-2|4|8G is that the values returned by the driver are in mG in all the 3 modes.

Quoted 6 lines

Corresponding to the 4G and 8G I got the details form older patches (SHIFT_ADJ_4G and SHIFT_ADJ_8G).
http://driverdev.linuxdriverproject.org/pipermail/devel/2010-November/009685.html

We can easily interpret number of bits for 4G and 8G from 2G information.

Going through the code of this driver i can see what you are talking about. Depending on the full-scale-range the device is being configured for, the number of bits used to represent acceleration in the driver is changed.

Again judging from the code, the driver is always returning acceleration at a constant accuracy i.e. 1mG in all the 3 modes (+/-2|4|8G)i.e.
+/-2G is mode means value can be anywhere from +/-2048mG, (requiring 12bits.)

(quoted diff) +2 -0
+/-4G in the range of +/-4096mG, requiring 13bits.
+/-8G i.e. +/-8192mG, requiring 14bits.
Quoted 5 lines

Was this done...

a. ...because LIS331DLH's theoretical MAX accuracy is ~1mG If yes, then using 12bits is fine.

Note from datasheet, "1LSb=4g/4096 at 12bit representation, ±2g Full-scale"

Quoted 1 line

From this I understood that ±2G full scale value is 12 bits.

That is one more reason to take 12bit value.

Regards
AnilKumar

#8 lore raw
Quoted 2 lines

Note from datasheet, "1LSb=4g/4096 at 12bit representation, ±2g Full-scale"

Precisely my point. All the datasheet says is :
1. It is +/-2G mode --> so Numerator is 4G.
2. We are using 12bits --> so Denominator is 2^12 = 4096.

There is no clear reason/justification as to why 12bits was chosen in the first place. At this point unless someone from the design/original-driver team actually confirms why the 12bit representation was chosen, it is all conjecture on our part.

If you have the actual LIS331DLH hardware, can you verify if the lower 4bits do actually contain any data (or are they always zero). This will help us decide whether to use them(16bit mode) or discard them(12bit mode)?...

regards
ChinmayVS

#9 lore raw

Chinmay,

Quoted 12 lines · Chinmay V S
Quoted 2 lines

Note from datasheet, "1LSb=4g/4096 at 12bit representation, ±2g Full-scale"

Precisely my point. All the datasheet says is :
1. It is +/-2G mode --> so Numerator is 4G.
2. We are using 12bits --> so Denominator is 2^12 = 4096.

There is no clear reason/justification as to why 12bits was chosen in the first place. At this point unless someone from the design/original-driver team actually confirms why the 12bit representation was chosen, it is all conjecture on our part.

Quoted 5 lines

If you have the actual LIS331DLH hardware, can you verify if the lower 4bits do actually contain any data (or are they always zero). This will help us decide whether to use them(16bit mode) or discard them(12bit mode)?...

This is the outdata of accelerometer

root@am335xevm:~# cat /sys/devices/platform/lis3lv02d/position
[   16.323852] lis3lv02d: hi 0x1    low 0x50   val 0x15
[   16.329742] lis3lv02d: hi 0x0    low 0x0    val 0x0
[   16.335052] lis3lv02d: hi 0x3e   low 0x50   val 0x3e5
(20,0,973)
root@am335xevm:~# cat /sys/devices/platform/lis3lv02d/position
[   25.777343] lis3lv02d: hi 0x2    low 0xa0   val 0x2a
[   25.783203] lis3lv02d: hi 0xc1   low 0xf0   val 0xfffffc1f
[   25.790130] lis3lv02d: hi 0xfd   low 0x50   val 0xffffffd5
(41,-969,-41)
root@am335xevm:~# cat /sys/devices/platform/lis3lv02d/position
[   47.607330] lis3lv02d: hi 0xc0   low 0x60   val 0xfffffc06
[   47.613464] lis3lv02d: hi 0xfd   low 0x90   val 0xffffffd9
[   47.619934] lis3lv02d: hi 0x2    low 0x40   val 0x24
(-994,-38,35)

Lower nibble always "0"

Regards
AnilKumar

#10 lore raw

12bit it IS then. :-)
LGTM. +1.

Quoted 43 lines · Chimata

Chinmay,

Quoted 12 lines · Chinmay V S
Quoted 2 lines

Note from datasheet, "1LSb=4g/4096 at 12bit representation, ±2g Full-scale"

Precisely my point. All the datasheet says is :
1. It is +/-2G mode --> so Numerator is 4G.
2. We are using 12bits --> so Denominator is 2^12 = 4096.

There is no clear reason/justification as to why 12bits was chosen in the first place. At this point unless someone from the design/original-driver team actually confirms why the 12bit representation was chosen, it is all conjecture on our part.

Quoted 5 lines

If you have the actual LIS331DLH hardware, can you verify if the lower 4bits do actually contain any data (or are they always zero). This will help us decide whether to use them(16bit mode) or discard them(12bit mode)?...

This is the outdata of accelerometer

root@am335xevm:~# cat /sys/devices/platform/lis3lv02d/position
[   16.323852] lis3lv02d: hi 0x1    low 0x50   val 0x15
[   16.329742] lis3lv02d: hi 0x0    low 0x0    val 0x0
[   16.335052] lis3lv02d: hi 0x3e   low 0x50   val 0x3e5
(20,0,973)
root@am335xevm:~# cat /sys/devices/platform/lis3lv02d/position
[   25.777343] lis3lv02d: hi 0x2    low 0xa0   val 0x2a
[   25.783203] lis3lv02d: hi 0xc1   low 0xf0   val 0xfffffc1f
[   25.790130] lis3lv02d: hi 0xfd   low 0x50   val 0xffffffd5
(41,-969,-41)
root@am335xevm:~# cat /sys/devices/platform/lis3lv02d/position
[   47.607330] lis3lv02d: hi 0xc0   low 0x60   val 0xfffffc06
[   47.613464] lis3lv02d: hi 0xfd   low 0x90   val 0xffffffd9
[   47.619934] lis3lv02d: hi 0x2    low 0x40   val 0x24
(-994,-38,35)

Lower nibble always "0"

Regards
AnilKumar

Signature · 2 lines
regards
ChinmayVS
#11 lore raw
In reply to Arnd Bergmann

Hi Eric,

Quoted 10 lines · Arnd Bergmann
Quoted 6 lines · AnilKumar Ch

This patch adds support for lis331dlh digital accelerometer to the lis3lv02d driver family. Adds ID field for detecting the lis331dlh module, based on this ID field lis3lv02d driver will export the lis331dlh module functionality.

If there are no further comments could you please push this patch?

Thanks
AnilKumar

maintainer Reviewed-by on v2 1/1
#12 lore raw
In reply to AnilKumar Ch
Quoted 4 lines · AnilKumar Ch

This patch adds support for lis331dlh digital accelerometer to the lis3lv02d driver family. Adds ID field for detecting the lis331dlh module, based on this ID field lis3lv02d driver will export the lis331dlh module functionality.

Hello AnilKumar,

Sorry for taking so long to review your patch. Overall, it looks great :-)

There are just a few points that almost code-style/comment that needs to be fixed. See below. Could you fix these small issues, and submit a new patch for Andrew to pick it in his queue?

Here is my:

Quoted 33 lines

---
Changes from v1:
- Removed G range sysfs entry from v1
- Modified documentation to specify lis331dlh is supported

  Documentation/misc-devices/lis3lv02d   |    3 ++-
  drivers/misc/lis3lv02d/lis3lv02d.c     |   42 +++++++++++++++++++++++++++++-
  drivers/misc/lis3lv02d/lis3lv02d.h     |   44 +++++++++++++++++++++++++++-----
  drivers/misc/lis3lv02d/lis3lv02d_i2c.c |    7 ++++-
  4 files changed, 87 insertions(+), 9 deletions(-)
Documentation/misc-devices/lis3lv02d +2 -1
index f1a4ec8..af815b9 100644
@@ -4,7 +4,8 @@ Kernel driver lis3lv02d
  Supported chips:
 
    * STMicroelectronics LIS3LV02DL, LIS3LV02DQ (12 bits precision)
-  * STMicroelectronics LIS302DL, LIS3L02DQ, LIS331DL (8 bits)
+  * STMicroelectronics LIS302DL, LIS3L02DQ, LIS331DL (8 bits) and
+    LIS331DLH (16 bits)
 
  Authors:
          Yan Burman <[email protected]>
drivers/misc/lis3lv02d/lis3lv02d.c +0 -0
index 29d12a7..c0a9199 100644
@@ -80,6 +80,14 @@
  #define LIS3_SENSITIVITY_12B		((LIS3_ACCURACY * 1000) / 1024)

drivers/misc/lis3lv02d/lis3lv02d.c line 90

Quoted 10 lines
drivers/misc/lis3lv02d/lis3lv02d.c +8 -0
  #define LIS3_SENSITIVITY_8B		(18 * LIS3_ACCURACY)
 
+/*
+ * LIS3331DLH spec says 1LSBs corresponds 4G/1024 -> 1LSB is 1000/1024 mG.
+ * Sensitivity values for +/-2G, outdata in 12 bits for +/-2G scale. so 4
+ * bits adjustment is required
+ */
+#define LIS3DLH_SENSITIVITY_2G		((LIS3_ACCURACY * 1000) / 1024)
+#define SHIFT_ADJ_2G			4
+

You said later:

Quoted 2 lines

Typo mistake this should be 4G/4096

Could you fix the comment. Even better, change LIS3331DLH to LIS331DLH

Also, if I understand correctly, there is currently no support for changing the sensitivity. It stays at 2G all the time, right? In such case, please add a sentence in this comment that the driver does only support the 2G sensitivity for now.

Quoted 9 lines
  #define LIS3_DEFAULT_FUZZ_12B		3
  #define LIS3_DEFAULT_FLAT_12B		3
  #define LIS3_DEFAULT_FUZZ_8B		1
drivers/misc/lis3lv02d/lis3lv02d.c +2 -0
@@ -133,6 +141,19 @@ static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
  	return (s16)((hi << 8) | lo);
  }
 
+/* 12bits for 2G range, 13 bits for 4G range and 14 bits for 8G range */
+static s16 lis3lv02d_read_16(struct lis3lv02d *lis3, int reg)

drivers/misc/lis3lv02d/lis3lv02d.c static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg) · line 155

Quoted 10 lines
drivers/misc/lis3lv02d/lis3lv02d.c +10 -0
+{
+	u8 lo, hi;
+	int v;
+
+	lis3->read(lis3, reg - 1, &lo);
+	lis3->read(lis3, reg, &hi);
+	v = (int) ((hi << 8) | lo);
+
+	return (s16) v >> lis3->shift_adj;
+}

As the method reads 12, 13, or 14 bits, it's a bit tricky to call it "*_read_16". I'd suggest maybe "lis3lv02d_read_12_14", "lis3lv02d_read_adj_16", or even "lis331dlh_read_data". Pick the one you prefer :-)

Quoted 155 lines
drivers/misc/lis3lv02d/lis3lv02d.c +20 -1
  /**
   * lis3lv02d_get_axis - For the given axis, give the value converted
   * @axis:      1,2,3 - can also be negative
@@ -193,6 +214,7 @@ static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
  static int lis3_12_rates[4] = {40, 160, 640, 2560};
  static int lis3_8_rates[2] = {100, 400};
  static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
+static int lis3_3dlh_rates[4] = {50, 100, 400, 1000};
 
  /* ODR is Output Data Rate */
  static int lis3lv02d_get_odr(struct lis3lv02d *lis3)
@@ -265,7 +287,7 @@ static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
  				(LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY));
  	}
 
-	if (lis3->whoami == WAI_3DC) {
+	if ((lis3->whoami == WAI_3DC) || (lis3->whoami == WAI_3DLH)) {
  		ctlreg = CTRL_REG4;
  		selftest = CTRL4_ST0;
  	} else {
@@ -396,9 +418,17 @@ int lis3lv02d_poweron(struct lis3lv02d *lis3)
  		lis3->read(lis3, CTRL_REG2, &reg);
  		if (lis3->whoami ==  WAI_12B)
  			reg |= CTRL2_BDU | CTRL2_BOOT;
+		else if (lis3->whoami ==  WAI_3DLH)
+			reg |= CTRL2_BOOT_3DLH;
  		else
  			reg |= CTRL2_BOOT_8B;
  		lis3->write(lis3, CTRL_REG2, reg);
+
+		if (lis3->whoami ==  WAI_3DLH) {
+			lis3->read(lis3, CTRL_REG4, &reg);
+			reg |= CTRL4_BDU;
+			lis3->write(lis3, CTRL_REG4, reg);
+		}
  	}
 
  	err = lis3lv02d_get_pwron_wait(lis3);
@@ -954,6 +984,16 @@ int lis3lv02d_init_device(struct lis3lv02d *lis3)
  		lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
  		lis3->scale = LIS3_SENSITIVITY_8B;
  		break;
+	case WAI_3DLH:
+		pr_info("16 bits 3DLH sensor found\n");
+		lis3->read_data = lis3lv02d_read_16;
+		lis3->mdps_max_val = 2048; /* 12 bits for 2G */
+		lis3->shift_adj = SHIFT_ADJ_2G;
+		lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
+		lis3->odrs = lis3_3dlh_rates;
+		lis3->odr_mask = CTRL1_DR0 | CTRL1_DR1;
+		lis3->scale = LIS3DLH_SENSITIVITY_2G;
+		break;
  	default:
  		pr_err("unknown sensor type 0x%X\n", lis3->whoami);
  		return -EINVAL;
drivers/misc/lis3lv02d/lis3lv02d.h +38 -6
index 2b1482a..c1a545e 100644
@@ -26,12 +26,12 @@
  /*
   * This driver tries to support the "digital" accelerometer chips from
   * STMicroelectronics such as LIS3LV02DL, LIS302DL, LIS3L02DQ, LIS331DL,
- * LIS35DE, or LIS202DL. They are very similar in terms of programming, with
- * almost the same registers. In addition to differing on physical properties,
- * they differ on the number of axes (2/3), precision (8/12 bits), and special
- * features (freefall detection, click...). Unfortunately, not all the
- * differences can be probed via a register.
- * They can be connected either via I²C or SPI.
+ * LIS331DLH, LIS35DE, or LIS202DL. They are very similar in terms of
+ * programming, with almost the same registers. In addition to differing
+ * on physical properties, they differ on the number of axes (2/3),
+ * precision (8/12 bits), and special features (freefall detection,
+ * click...). Unfortunately, not all the differences can be probed via
+ * a register. They can be connected either via I²C or SPI.
   */
 
  #include <linux/lis3lv02d.h>
@@ -96,12 +96,22 @@ enum lis3lv02d_reg {
  };
 
  enum lis3_who_am_i {
+	WAI_3DLH	= 0x32,	/* 16 bits: LIS331DLH */
  	WAI_3DC		= 0x33,	/* 8 bits: LIS3DC, HP3DC */
  	WAI_12B		= 0x3A, /* 12 bits: LIS3LV02D[LQ]... */
  	WAI_8B		= 0x3B, /* 8 bits: LIS[23]02D[LQ]... */
  	WAI_6B		= 0x52, /* 6 bits: LIS331DLF - not supported */
  };
 
+enum lis3_type {
+	LIS3DC,
+	HP3DC,
+	LIS3LV02D,
+	LIS2302D,
+	LIS331DLF,
+	LIS331DLH,
+};
+
  enum lis3lv02d_ctrl1_12b {
  	CTRL1_Xen	= 0x01,
  	CTRL1_Yen	= 0x02,
@@ -129,6 +139,27 @@ enum lis3lv02d_ctrl1_3dc {
  	CTRL1_ODR3	= 0x80,
  };
 
+enum lis331dlh_ctrl1 {
+	CTRL1_DR0	= 0x08,
+	CTRL1_DR1	= 0x10,
+	CTRL1_PM0	= 0x20,
+	CTRL1_PM1	= 0x40,
+	CTRL1_PM2	= 0x80,
+};
+
+enum lis331dlh_ctrl2 {
+	CTRL2_HPEN1	= 0x04,
+	CTRL2_HPEN2	= 0x08,
+	CTRL2_FDS_3DLH	= 0x10,
+	CTRL2_BOOT_3DLH	= 0x80,
+};
+
+enum lis331dlh_ctrl4 {
+	CTRL4_STSIGN	= 0x08,
+	CTRL4_BLE	= 0x40,
+	CTRL4_BDU	= 0x80,
+};
+
  enum lis3lv02d_ctrl2 {
  	CTRL2_DAS	= 0x01,
  	CTRL2_SIM	= 0x02,
@@ -279,6 +310,7 @@ struct lis3lv02d {
  	int                     data_ready_count[2];
  	atomic_t		wake_thread;
  	unsigned char           irq_cfg;
+	unsigned int		shift_adj;
 
  	struct lis3lv02d_platform_data *pdata;	/* for passing board config */
  	struct mutex		mutex;     /* Serialize poll and selftest */
drivers/misc/lis3lv02d/lis3lv02d_i2c.c +5 -1
index c02fea0..98cf9ba 100644
@@ -90,7 +90,11 @@ static int lis3_i2c_init(struct lis3lv02d *lis3)
  	if (ret < 0)
  		return ret;
 
-	reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+	if (lis3->whoami == WAI_3DLH)
+		reg |= CTRL1_PM0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+	else
+		reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+
  	return lis3->write(lis3, CTRL_REG1, reg);
  }
 

drivers/misc/lis3lv02d/lis3lv02d_i2c.c static int lis3_i2c_runtime_resume(struct device *dev) · line 239

Quoted 4 lines
drivers/misc/lis3lv02d/lis3lv02d_i2c.c +1 -0
@@ -232,6 +236,7 @@ static int lis3_i2c_runtime_resume(struct device *dev)
 
  static const struct i2c_device_id lis3lv02d_id[] = {
  	{"lis3lv02d", 0 },
+	{"lis331dlh", LIS331DLH},

I'm not fully grasping the meaning of this table. But as there seems to be no user of the second value, I imagine this value just has to be different for each entry? If so, I'd recommend to change the 0 to LIS3LV02D, to make it clear that LIS331DLH != 0. Or is this value meaningful for the user, in which case we cannot change it anymore?

Cheers,
Éric

#13 lore raw

Hi Eric,

Thanks for the comments,

Quoted 6 lines · Éric Piel
Quoted 4 lines · AnilKumar Ch

This patch adds support for lis331dlh digital accelerometer to the lis3lv02d driver family. Adds ID field for detecting the lis331dlh module, based on this ID field lis3lv02d driver will export the lis331dlh module functionality.

Quoted 7 lines

Hello AnilKumar,

Sorry for taking so long to review your patch. Overall, it looks great :-)

There are just a few points that almost code-style/comment that needs to be fixed. See below. Could you fix these small issues, and submit a new patch for Andrew to pick it in his queue?

As I can see this patch is already in linux-next, I will submit a new patch by addressing all your comments.

Quoted 50 lines

Here is my:

Quoted 33 lines

---
Changes from v1:
- Removed G range sysfs entry from v1
- Modified documentation to specify lis331dlh is supported

  Documentation/misc-devices/lis3lv02d   |    3 ++-
  drivers/misc/lis3lv02d/lis3lv02d.c     |   42 +++++++++++++++++++++++++++++-
  drivers/misc/lis3lv02d/lis3lv02d.h     |   44 +++++++++++++++++++++++++++-----
  drivers/misc/lis3lv02d/lis3lv02d_i2c.c |    7 ++++-
  4 files changed, 87 insertions(+), 9 deletions(-)
Documentation/misc-devices/lis3lv02d +2 -1
index f1a4ec8..af815b9 100644
@@ -4,7 +4,8 @@ Kernel driver lis3lv02d
  Supported chips:
 
    * STMicroelectronics LIS3LV02DL, LIS3LV02DQ (12 bits precision)
-  * STMicroelectronics LIS302DL, LIS3L02DQ, LIS331DL (8 bits)
+  * STMicroelectronics LIS302DL, LIS3L02DQ, LIS331DL (8 bits) and
+    LIS331DLH (16 bits)
 
  Authors:
          Yan Burman <[email protected]>
drivers/misc/lis3lv02d/lis3lv02d.c +0 -0
index 29d12a7..c0a9199 100644
@@ -80,6 +80,14 @@
  #define LIS3_SENSITIVITY_12B		((LIS3_ACCURACY * 1000) / 1024)

drivers/misc/lis3lv02d/lis3lv02d.c line 90

Quoted 10 lines
drivers/misc/lis3lv02d/lis3lv02d.c +8 -0
  #define LIS3_SENSITIVITY_8B		(18 * LIS3_ACCURACY)
 
+/*
+ * LIS3331DLH spec says 1LSBs corresponds 4G/1024 -> 1LSB is 1000/1024 mG.
+ * Sensitivity values for +/-2G, outdata in 12 bits for +/-2G scale. so 4
+ * bits adjustment is required
+ */
+#define LIS3DLH_SENSITIVITY_2G		((LIS3_ACCURACY * 1000) / 1024)
+#define SHIFT_ADJ_2G			4
+

You said later:

Quoted 1 line
Quoted 7 lines
Quoted 1 line

Typo mistake this should be 4G/4096

Could you fix the comment. Even better, change LIS3331DLH to LIS331DLH

Also, if I understand correctly, there is currently no support for changing the sensitivity. It stays at 2G all the time, right? In such case, please add a sentence in this comment that the driver does only support the 2G sensitivity for now.

I will do this

Quoted 9 lines
Quoted 9 lines

  #define LIS3_DEFAULT_FUZZ_12B		3
  #define LIS3_DEFAULT_FLAT_12B		3
  #define LIS3_DEFAULT_FUZZ_8B		1
drivers/misc/lis3lv02d/lis3lv02d.c +0 -0
@@ -133,6 +141,19 @@ static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
  	return (s16)((hi << 8) | lo);
  }
 
Quoted 16 lines

drivers/misc/lis3lv02d/lis3lv02d.c static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg) · line 155

Quoted 12 lines
drivers/misc/lis3lv02d/lis3lv02d.c +12 -0
+/* 12bits for 2G range, 13 bits for 4G range and 14 bits for 8G range */
+static s16 lis3lv02d_read_16(struct lis3lv02d *lis3, int reg)
+{
+	u8 lo, hi;
+	int v;
+
+	lis3->read(lis3, reg - 1, &lo);
+	lis3->read(lis3, reg, &hi);
+	v = (int) ((hi << 8) | lo);
+
+	return (s16) v >> lis3->shift_adj;
+}

As the method reads 12, 13, or 14 bits, it's a bit tricky to call it "*_read_16". I'd suggest maybe "lis3lv02d_read_12_14", "lis3lv02d_read_adj_16", or even "lis331dlh_read_data". Pick the one you prefer :-)

Comment is available to convey the number of bits so I am changing it to "lis331dlh_read_data".

Quoted 39 lines
Quoted 39 lines
drivers/misc/lis3lv02d/lis3lv02d.c +10 -1
  /**
   * lis3lv02d_get_axis - For the given axis, give the value converted
   * @axis:      1,2,3 - can also be negative
@@ -193,6 +214,7 @@ static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
  static int lis3_12_rates[4] = {40, 160, 640, 2560};
  static int lis3_8_rates[2] = {100, 400};
  static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
+static int lis3_3dlh_rates[4] = {50, 100, 400, 1000};
 
  /* ODR is Output Data Rate */
  static int lis3lv02d_get_odr(struct lis3lv02d *lis3)
@@ -265,7 +287,7 @@ static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
  				(LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY));
  	}
 
-	if (lis3->whoami == WAI_3DC) {
+	if ((lis3->whoami == WAI_3DC) || (lis3->whoami == WAI_3DLH)) {
  		ctlreg = CTRL_REG4;
  		selftest = CTRL4_ST0;
  	} else {
@@ -396,9 +418,17 @@ int lis3lv02d_poweron(struct lis3lv02d *lis3)
  		lis3->read(lis3, CTRL_REG2, &reg);
  		if (lis3->whoami ==  WAI_12B)
  			reg |= CTRL2_BDU | CTRL2_BOOT;
+		else if (lis3->whoami ==  WAI_3DLH)
+			reg |= CTRL2_BOOT_3DLH;
  		else
  			reg |= CTRL2_BOOT_8B;
  		lis3->write(lis3, CTRL_REG2, reg);
+
+		if (lis3->whoami ==  WAI_3DLH) {
+			lis3->read(lis3, CTRL_REG4, &reg);
+			reg |= CTRL4_BDU;
+			lis3->write(lis3, CTRL_REG4, reg);
+		}
  	}
 
Quoted 7 lines

drivers/misc/lis3lv02d/lis3lv02d.c int lis3lv02d_init_device(struct lis3lv02d *lis3) · line 988

Quoted 7 lines
drivers/misc/lis3lv02d/lis3lv02d.c +2 -0
  	err = lis3lv02d_get_pwron_wait(lis3);
@@ -954,6 +984,16 @@ int lis3lv02d_init_device(struct lis3lv02d *lis3)
  		lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
  		lis3->scale = LIS3_SENSITIVITY_8B;
  		break;
+	case WAI_3DLH:
+		pr_info("16 bits 3DLH sensor found\n");

I am planning to change this print message to
"LIS331DLH sensor found"

Quoted 112 lines
Quoted 112 lines
drivers/misc/lis3lv02d/lis3lv02d.c +8 -0
+		lis3->read_data = lis3lv02d_read_16;
+		lis3->mdps_max_val = 2048; /* 12 bits for 2G */
+		lis3->shift_adj = SHIFT_ADJ_2G;
+		lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
+		lis3->odrs = lis3_3dlh_rates;
+		lis3->odr_mask = CTRL1_DR0 | CTRL1_DR1;
+		lis3->scale = LIS3DLH_SENSITIVITY_2G;
+		break;
  	default:
  		pr_err("unknown sensor type 0x%X\n", lis3->whoami);
  		return -EINVAL;
drivers/misc/lis3lv02d/lis3lv02d.h +38 -6
index 2b1482a..c1a545e 100644
@@ -26,12 +26,12 @@
  /*
   * This driver tries to support the "digital" accelerometer chips from
   * STMicroelectronics such as LIS3LV02DL, LIS302DL, LIS3L02DQ, LIS331DL,
- * LIS35DE, or LIS202DL. They are very similar in terms of programming, with
- * almost the same registers. In addition to differing on physical properties,
- * they differ on the number of axes (2/3), precision (8/12 bits), and special
- * features (freefall detection, click...). Unfortunately, not all the
- * differences can be probed via a register.
- * They can be connected either via I²C or SPI.
+ * LIS331DLH, LIS35DE, or LIS202DL. They are very similar in terms of
+ * programming, with almost the same registers. In addition to differing
+ * on physical properties, they differ on the number of axes (2/3),
+ * precision (8/12 bits), and special features (freefall detection,
+ * click...). Unfortunately, not all the differences can be probed via
+ * a register. They can be connected either via I²C or SPI.
   */
 
  #include <linux/lis3lv02d.h>
@@ -96,12 +96,22 @@ enum lis3lv02d_reg {
  };
 
  enum lis3_who_am_i {
+	WAI_3DLH	= 0x32,	/* 16 bits: LIS331DLH */
  	WAI_3DC		= 0x33,	/* 8 bits: LIS3DC, HP3DC */
  	WAI_12B		= 0x3A, /* 12 bits: LIS3LV02D[LQ]... */
  	WAI_8B		= 0x3B, /* 8 bits: LIS[23]02D[LQ]... */
  	WAI_6B		= 0x52, /* 6 bits: LIS331DLF - not supported */
  };
 
+enum lis3_type {
+	LIS3DC,
+	HP3DC,
+	LIS3LV02D,
+	LIS2302D,
+	LIS331DLF,
+	LIS331DLH,
+};
+
  enum lis3lv02d_ctrl1_12b {
  	CTRL1_Xen	= 0x01,
  	CTRL1_Yen	= 0x02,
@@ -129,6 +139,27 @@ enum lis3lv02d_ctrl1_3dc {
  	CTRL1_ODR3	= 0x80,
  };
 
+enum lis331dlh_ctrl1 {
+	CTRL1_DR0	= 0x08,
+	CTRL1_DR1	= 0x10,
+	CTRL1_PM0	= 0x20,
+	CTRL1_PM1	= 0x40,
+	CTRL1_PM2	= 0x80,
+};
+
+enum lis331dlh_ctrl2 {
+	CTRL2_HPEN1	= 0x04,
+	CTRL2_HPEN2	= 0x08,
+	CTRL2_FDS_3DLH	= 0x10,
+	CTRL2_BOOT_3DLH	= 0x80,
+};
+
+enum lis331dlh_ctrl4 {
+	CTRL4_STSIGN	= 0x08,
+	CTRL4_BLE	= 0x40,
+	CTRL4_BDU	= 0x80,
+};
+
  enum lis3lv02d_ctrl2 {
  	CTRL2_DAS	= 0x01,
  	CTRL2_SIM	= 0x02,
@@ -279,6 +310,7 @@ struct lis3lv02d {
  	int                     data_ready_count[2];
  	atomic_t		wake_thread;
  	unsigned char           irq_cfg;
+	unsigned int		shift_adj;
 
  	struct lis3lv02d_platform_data *pdata;	/* for passing board config */
  	struct mutex		mutex;     /* Serialize poll and selftest */
drivers/misc/lis3lv02d/lis3lv02d_i2c.c +5 -1
index c02fea0..98cf9ba 100644
@@ -90,7 +90,11 @@ static int lis3_i2c_init(struct lis3lv02d *lis3)
  	if (ret < 0)
  		return ret;
 
-	reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+	if (lis3->whoami == WAI_3DLH)
+		reg |= CTRL1_PM0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+	else
+		reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
+
  	return lis3->write(lis3, CTRL_REG1, reg);
  }
 
@@ -232,6 +236,7 @@ static int lis3_i2c_runtime_resume(struct device *dev)
 
Quoted 9 lines

drivers/misc/lis3lv02d/lis3lv02d_i2c.c static int lis3_i2c_runtime_resume(struct device *dev) · line 239

Quoted 3 lines
drivers/misc/lis3lv02d/lis3lv02d_i2c.c +1 -0
  static const struct i2c_device_id lis3lv02d_id[] = {
  	{"lis3lv02d", 0 },
+	{"lis331dlh", LIS331DLH},

I'm not fully grasping the meaning of this table. But as there seems to be no user of the second value, I imagine this value just has to be different for each entry? If so, I'd recommend to change the 0 to LIS3LV02D, to make it clear that LIS331DLH != 0. Or is this value meaningful for the user, in which case we cannot change it anymore?

I will change this and update the enum lis3_type accordingly, which has these macros

Thanks
AnilKumar