23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/types.h>
34 #include <linux/wait.h>
35 #include <linux/poll.h>
36 #include <linux/slab.h>
45 #define DRIVER_NAME "lis3lv02d"
48 #define MDPS_POLL_INTERVAL 50
49 #define MDPS_POLL_MIN 0
50 #define MDPS_POLL_MAX 2000
52 #define LIS3_SYSFS_POWERDOWN_DELAY 5000
55 #define SELFTEST_FAIL -1
56 #define SELFTEST_IRQ -2
70 #define LIS3_PWRON_DELAY_WAI_12B (5000)
71 #define LIS3_PWRON_DELAY_WAI_8B (3000)
79 #define LIS3_ACCURACY 1024
81 #define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024)
82 #define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY)
90 #define LIS3DLH_SENSITIVITY_2G ((LIS3_ACCURACY * 1000) / 1024)
91 #define SHIFT_ADJ_2G 4
93 #define LIS3_DEFAULT_FUZZ_12B 3
94 #define LIS3_DEFAULT_FLAT_12B 3
95 #define LIS3_DEFAULT_FUZZ_8B 1
96 #define LIS3_DEFAULT_FLAT_8B 1
106 static int param_set_axis(
const char *
val,
const struct kernel_param *kp)
110 int val = *(
int *)kp->
arg;
120 .set = param_set_axis,
124 #define param_check_axis(name, p) param_check_int(name, p)
132 if (lis3->
read(lis3, reg, &lo) < 0)
138 static s16 lis3lv02d_read_12(
struct lis3lv02d *lis3,
int reg)
142 lis3->
read(lis3, reg - 1, &lo);
143 lis3->
read(lis3, reg, &hi);
145 return (
s16)((hi << 8) | lo);
149 static s16 lis331dlh_read_data(
struct lis3lv02d *lis3,
int reg)
154 lis3->
read(lis3, reg - 1, &lo);
155 lis3->
read(lis3, reg, &hi);
156 v = (
int) ((hi << 8) | lo);
168 static inline int lis3lv02d_get_axis(
s8 axis,
int hw_values[3])
171 return hw_values[axis - 1];
173 return -hw_values[-axis - 1];
185 static void lis3lv02d_get_xyz(
struct lis3lv02d *lis3,
int *
x,
int *
y,
int *z)
194 for (i = 0; i < 3; i++)
200 for (i = 0; i < 3; i++)
201 position[i] = (
s8)data[i * 2];
209 for (i = 0; i < 3; i++)
212 *x = lis3lv02d_get_axis(lis3->
ac.
x, position);
213 *y = lis3lv02d_get_axis(lis3->
ac.
y, position);
214 *z = lis3lv02d_get_axis(lis3->
ac.
z, position);
218 static int lis3_12_rates[4] = {40, 160, 640, 2560};
219 static int lis3_8_rates[2] = {100, 400};
220 static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
221 static int lis3_3dlh_rates[4] = {50, 100, 400, 1000};
224 static int lis3lv02d_get_odr(
struct lis3lv02d *lis3)
232 return lis3->
odrs[(ctrl >> shift)];
235 static int lis3lv02d_get_pwron_wait(
struct lis3lv02d *lis3)
237 int div = lis3lv02d_get_odr(lis3);
239 if (
WARN_ONCE(div == 0,
"device returned spurious data"))
247 static int lis3lv02d_set_odr(
struct lis3lv02d *lis3,
int rate)
257 len = 1 << hweight_long(lis3->
odr_mask);
260 for (i = 0; i < len; i++)
261 if (lis3->
odrs[i] == rate) {
263 ctrl | (i << shift));
269 static int lis3lv02d_selftest(
struct lis3lv02d *lis3,
s16 results[3])
305 lis3->
read(lis3, ctlreg, ®);
306 lis3->
write(lis3, ctlreg, (reg | selftest));
307 ret = lis3lv02d_get_pwron_wait(lis3);
317 lis3->
write(lis3, ctlreg, reg);
318 ret = lis3lv02d_get_pwron_wait(lis3);
349 for (i = 0; i < 3; i++) {
351 if ((results[i] < lis3->
pdata->st_min_limits[i]) ||
352 (results[i] > lis3->
pdata->st_max_limits[i])) {
381 static inline void lis3_context_save(
struct lis3lv02d *lis3)
389 static inline void lis3_context_restore(
struct lis3lv02d *lis3)
400 lis3_context_save(lis3);
438 err = lis3lv02d_get_pwron_wait(lis3);
443 lis3_context_restore(lis3);
450 static void lis3lv02d_joystick_poll(
struct input_polled_dev *pidev)
456 lis3lv02d_get_xyz(lis3, &x, &y, &z);
457 input_report_abs(pidev->input,
ABS_X, x);
458 input_report_abs(pidev->input,
ABS_Y, y);
459 input_report_abs(pidev->input,
ABS_Z, z);
460 input_sync(pidev->input);
464 static void lis3lv02d_joystick_open(
struct input_polled_dev *pidev)
469 pm_runtime_get_sync(lis3->
pm_dev);
477 lis3lv02d_joystick_poll(pidev);
480 static void lis3lv02d_joystick_close(
struct input_polled_dev *pidev)
486 pm_runtime_put(lis3->
pm_dev);
511 static void lis302dl_interrupt_handle_click(
struct lis3lv02d *lis3)
513 struct input_dev *
dev = lis3->
idev->input;
537 static inline void lis302dl_data_ready(
struct lis3lv02d *lis3,
int index)
542 lis3lv02d_get_xyz(lis3, &dummy, &dummy, &dummy);
546 static irqreturn_t lis302dl_interrupt_thread1_8b(
int irq,
void *data)
552 lis302dl_interrupt_handle_click(lis3);
556 lis3lv02d_joystick_poll(lis3->
idev);
561 static irqreturn_t lis302dl_interrupt_thread2_8b(
int irq,
void *data)
567 lis302dl_interrupt_handle_click(lis3);
571 lis3lv02d_joystick_poll(lis3->
idev);
585 pm_runtime_get_sync(lis3->
pm_dev);
591 static int lis3lv02d_misc_release(
struct inode *inode,
struct file *file)
599 pm_runtime_put(lis3->
pm_dev);
603 static ssize_t lis3lv02d_misc_read(
struct file *file,
char __user *
buf,
611 unsigned char byte_data;
655 static unsigned int lis3lv02d_misc_poll(
struct file *file,
poll_table *
wait)
666 static int lis3lv02d_misc_fasync(
int fd,
struct file *file,
int on)
677 .read = lis3lv02d_misc_read,
678 .open = lis3lv02d_misc_open,
679 .release = lis3lv02d_misc_release,
680 .poll = lis3lv02d_misc_poll,
681 .fasync = lis3lv02d_misc_fasync,
686 struct input_dev *input_dev;
688 int max_val, fuzz, flat;
698 lis3->
idev->poll = lis3lv02d_joystick_poll;
699 lis3->
idev->open = lis3lv02d_joystick_open;
700 lis3->
idev->close = lis3lv02d_joystick_close;
704 lis3->
idev->private = lis3;
705 input_dev = lis3->
idev->input;
707 input_dev->name =
"ST LIS3LV02DL Accelerometer";
710 input_dev->id.vendor = 0;
711 input_dev->dev.parent = &lis3->
pdev->dev;
725 input_set_abs_params(input_dev,
ABS_X, -max_val, max_val, fuzz, flat);
726 input_set_abs_params(input_dev,
ABS_Y, -max_val, max_val, fuzz, flat);
727 input_set_abs_params(input_dev,
ABS_Z, -max_val, max_val, fuzz, flat);
762 static void lis3lv02d_sysfs_poweron(
struct lis3lv02d *lis3)
773 pm_runtime_get_sync(lis3->
pm_dev);
774 pm_runtime_put_noidle(lis3->
pm_dev);
785 static const char ok[] =
"OK";
786 static const char fail[] =
"FAIL";
787 static const char irq[] =
"FAIL_IRQ";
790 lis3lv02d_sysfs_poweron(lis3);
791 switch (lis3lv02d_selftest(lis3, values)) {
803 return sprintf(buf,
"%s %d %d %d\n", res,
804 values[0], values[1], values[2]);
813 lis3lv02d_sysfs_poweron(lis3);
815 lis3lv02d_get_xyz(lis3, &x, &y, &z);
817 return sprintf(buf,
"(%d,%d,%d)\n", x, y, z);
825 lis3lv02d_sysfs_poweron(lis3);
826 return sprintf(buf,
"%d\n", lis3lv02d_get_odr(lis3));
839 lis3lv02d_sysfs_poweron(lis3);
840 if (lis3lv02d_set_odr(lis3, rate))
851 static struct attribute *lis3lv02d_attributes[] = {
852 &dev_attr_selftest.attr,
853 &dev_attr_position.attr,
859 .attrs = lis3lv02d_attributes
863 static int lis3lv02d_add_fs(
struct lis3lv02d *lis3)
866 if (IS_ERR(lis3->
pdev))
867 return PTR_ERR(lis3->
pdev);
869 platform_set_drvdata(lis3->
pdev, lis3);
882 if (!pm_runtime_suspended(lis3->
pm_dev))
885 pm_runtime_disable(lis3->
pm_dev);
886 pm_runtime_set_suspended(lis3->
pm_dev);
893 static void lis3lv02d_8b_configure(
struct lis3lv02d *lis3,
910 struct input_dev *input_dev = lis3->
idev->input;
938 lis302dl_interrupt_thread2_8b,
943 pr_err(
"No second IRQ. Limited functionality\n");
976 if (!of_property_read_u32(np,
"st,click-threshold-x", &val))
978 if (!of_property_read_u32(np,
"st,click-threshold-y", &val))
980 if (!of_property_read_u32(np,
"st,click-threshold-z", &val))
983 if (!of_property_read_u32(np,
"st,click-time-limit", &val))
985 if (!of_property_read_u32(np,
"st,click-latency", &val))
987 if (!of_property_read_u32(np,
"st,click-window", &val))
1017 if (!of_property_read_u32(np,
"st,wu-duration-1", &val))
1019 if (!of_property_read_u32(np,
"st,wu-duration-2", &val))
1035 if (!of_property_read_u32(np,
"st,highpass-cutoff-hz", &val)) {
1109 pr_info(
"12 bits sensor found\n");
1113 lis3->
odrs = lis3_12_rates;
1116 lis3->
regs = lis3_wai12_regs;
1120 pr_info(
"8 bits sensor found\n");
1124 lis3->
odrs = lis3_8_rates;
1127 lis3->
regs = lis3_wai8_regs;
1131 pr_info(
"8 bits 3DC sensor found\n");
1135 lis3->
odrs = lis3_3dc_rates;
1140 pr_info(
"16 bits lis331dlh sensor found\n");
1145 lis3->
odrs = lis3_3dlh_rates;
1165 lis3lv02d_add_fs(lis3);
1173 pm_runtime_set_active(lis3->
pm_dev);
1178 pr_err(
"joystick initialization failed\n");
1186 lis3lv02d_8b_configure(lis3, p);
1200 pr_debug(
"No IRQ. Disabling /dev/freefall\n");
1216 thread_fn = lis302dl_interrupt_thread1_8b;
1227 pr_err(
"Cannot get IRQ\n");
1232 lis3->
miscdev.name =
"freefall";
1233 lis3->
miscdev.fops = &lis3lv02d_misc_fops;
1236 pr_err(
"misc_register failed\n");