Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * STMicroelectronics accelerometers driver |
| 3 | * |
| 4 | * Copyright 2012-2013 STMicroelectronics Inc. |
| 5 | * |
| 6 | * Denis Ciocca <denis.ciocca@st.com> |
| 7 | * |
| 8 | * Licensed under the GPL-2. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
Shrirang Bagul | 89a2a93f | 2016-11-24 13:33:44 +0800 | [diff] [blame] | 14 | #include <linux/acpi.h> |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 15 | #include <linux/i2c.h> |
| 16 | #include <linux/iio/iio.h> |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 17 | |
| 18 | #include <linux/iio/common/st_sensors.h> |
| 19 | #include <linux/iio/common/st_sensors_i2c.h> |
| 20 | #include "st_accel.h" |
| 21 | |
Linus Walleij | 2d7768a | 2014-08-07 08:16:00 +0100 | [diff] [blame] | 22 | #ifdef CONFIG_OF |
| 23 | static const struct of_device_id st_accel_of_match[] = { |
| 24 | { |
Linus Walleij | 7622277 | 2017-01-05 14:32:33 +0100 | [diff] [blame] | 25 | /* An older compatible */ |
| 26 | .compatible = "st,lis3lv02d", |
| 27 | .data = LIS3LV02DL_ACCEL_DEV_NAME, |
| 28 | }, |
| 29 | { |
Linus Walleij | 3acddf7 | 2015-03-18 10:52:06 +0100 | [diff] [blame] | 30 | .compatible = "st,lis3lv02dl-accel", |
| 31 | .data = LIS3LV02DL_ACCEL_DEV_NAME, |
| 32 | }, |
| 33 | { |
Linus Walleij | 2d7768a | 2014-08-07 08:16:00 +0100 | [diff] [blame] | 34 | .compatible = "st,lsm303dlh-accel", |
| 35 | .data = LSM303DLH_ACCEL_DEV_NAME, |
| 36 | }, |
| 37 | { |
| 38 | .compatible = "st,lsm303dlhc-accel", |
| 39 | .data = LSM303DLHC_ACCEL_DEV_NAME, |
| 40 | }, |
| 41 | { |
| 42 | .compatible = "st,lis3dh-accel", |
| 43 | .data = LIS3DH_ACCEL_DEV_NAME, |
| 44 | }, |
| 45 | { |
| 46 | .compatible = "st,lsm330d-accel", |
| 47 | .data = LSM330D_ACCEL_DEV_NAME, |
| 48 | }, |
| 49 | { |
| 50 | .compatible = "st,lsm330dl-accel", |
| 51 | .data = LSM330DL_ACCEL_DEV_NAME, |
| 52 | }, |
| 53 | { |
| 54 | .compatible = "st,lsm330dlc-accel", |
| 55 | .data = LSM330DLC_ACCEL_DEV_NAME, |
| 56 | }, |
| 57 | { |
Linus Walleij | bbf5f03 | 2015-05-19 15:37:18 +0200 | [diff] [blame] | 58 | .compatible = "st,lis331dl-accel", |
| 59 | .data = LIS331DL_ACCEL_DEV_NAME, |
| 60 | }, |
| 61 | { |
Linus Walleij | 2d7768a | 2014-08-07 08:16:00 +0100 | [diff] [blame] | 62 | .compatible = "st,lis331dlh-accel", |
| 63 | .data = LIS331DLH_ACCEL_DEV_NAME, |
| 64 | }, |
| 65 | { |
| 66 | .compatible = "st,lsm303dl-accel", |
| 67 | .data = LSM303DL_ACCEL_DEV_NAME, |
| 68 | }, |
| 69 | { |
| 70 | .compatible = "st,lsm303dlm-accel", |
| 71 | .data = LSM303DLM_ACCEL_DEV_NAME, |
| 72 | }, |
| 73 | { |
| 74 | .compatible = "st,lsm330-accel", |
| 75 | .data = LSM330_ACCEL_DEV_NAME, |
| 76 | }, |
Giuseppe Barba | ddc05fa | 2015-07-21 10:35:44 +0200 | [diff] [blame] | 77 | { |
| 78 | .compatible = "st,lsm303agr-accel", |
| 79 | .data = LSM303AGR_ACCEL_DEV_NAME, |
| 80 | }, |
Giuseppe Barba | 34dc578 | 2015-11-12 08:36:49 +0100 | [diff] [blame] | 81 | { |
| 82 | .compatible = "st,lis2dh12-accel", |
| 83 | .data = LIS2DH12_ACCEL_DEV_NAME, |
| 84 | }, |
Tiberiu Breana | 1e52fef | 2016-03-09 14:06:14 +0200 | [diff] [blame] | 85 | { |
| 86 | .compatible = "st,h3lis331dl-accel", |
Lorenzo Bianconi | b30f159 | 2017-07-08 13:00:18 +0200 | [diff] [blame^] | 87 | .data = H3LIS331DL_ACCEL_DEV_NAME, |
Tiberiu Breana | 1e52fef | 2016-03-09 14:06:14 +0200 | [diff] [blame] | 88 | }, |
Jonathan Cameron | 4e68cfb | 2016-05-22 20:39:29 +0100 | [diff] [blame] | 89 | { |
| 90 | .compatible = "st,lis3l02dq", |
| 91 | .data = LIS3L02DQ_ACCEL_DEV_NAME, |
| 92 | }, |
Lorenzo Bianconi | dcdb0a7 | 2016-10-25 23:09:03 +0200 | [diff] [blame] | 93 | { |
| 94 | .compatible = "st,lng2dm-accel", |
| 95 | .data = LNG2DM_ACCEL_DEV_NAME, |
| 96 | }, |
Linus Walleij | 2d7768a | 2014-08-07 08:16:00 +0100 | [diff] [blame] | 97 | {}, |
| 98 | }; |
| 99 | MODULE_DEVICE_TABLE(of, st_accel_of_match); |
| 100 | #else |
| 101 | #define st_accel_of_match NULL |
| 102 | #endif |
| 103 | |
Shrirang Bagul | 89a2a93f | 2016-11-24 13:33:44 +0800 | [diff] [blame] | 104 | #ifdef CONFIG_ACPI |
| 105 | static const struct acpi_device_id st_accel_acpi_match[] = { |
| 106 | {"SMO8A90", LNG2DM}, |
| 107 | { }, |
| 108 | }; |
| 109 | MODULE_DEVICE_TABLE(acpi, st_accel_acpi_match); |
| 110 | #else |
| 111 | #define st_accel_acpi_match NULL |
| 112 | #endif |
| 113 | |
| 114 | static const struct i2c_device_id st_accel_id_table[] = { |
| 115 | { LSM303DLH_ACCEL_DEV_NAME, LSM303DLH }, |
| 116 | { LSM303DLHC_ACCEL_DEV_NAME, LSM303DLHC }, |
| 117 | { LIS3DH_ACCEL_DEV_NAME, LIS3DH }, |
| 118 | { LSM330D_ACCEL_DEV_NAME, LSM330D }, |
| 119 | { LSM330DL_ACCEL_DEV_NAME, LSM330DL }, |
| 120 | { LSM330DLC_ACCEL_DEV_NAME, LSM330DLC }, |
| 121 | { LIS331DLH_ACCEL_DEV_NAME, LIS331DLH }, |
| 122 | { LSM303DL_ACCEL_DEV_NAME, LSM303DL }, |
| 123 | { LSM303DLM_ACCEL_DEV_NAME, LSM303DLM }, |
| 124 | { LSM330_ACCEL_DEV_NAME, LSM330 }, |
| 125 | { LSM303AGR_ACCEL_DEV_NAME, LSM303AGR }, |
| 126 | { LIS2DH12_ACCEL_DEV_NAME, LIS2DH12 }, |
| 127 | { LIS3L02DQ_ACCEL_DEV_NAME, LIS3L02DQ }, |
| 128 | { LNG2DM_ACCEL_DEV_NAME, LNG2DM }, |
Lorenzo Bianconi | b30f159 | 2017-07-08 13:00:18 +0200 | [diff] [blame^] | 129 | { H3LIS331DL_ACCEL_DEV_NAME, H3LIS331DL }, |
Lorenzo Bianconi | 6011208 | 2017-07-08 13:00:16 +0200 | [diff] [blame] | 130 | { LIS331DL_ACCEL_DEV_NAME, LIS331DL }, |
| 131 | { LIS3LV02DL_ACCEL_DEV_NAME, LIS3LV02DL }, |
Shrirang Bagul | 89a2a93f | 2016-11-24 13:33:44 +0800 | [diff] [blame] | 132 | {}, |
| 133 | }; |
| 134 | MODULE_DEVICE_TABLE(i2c, st_accel_id_table); |
| 135 | |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 136 | static int st_accel_i2c_probe(struct i2c_client *client, |
| 137 | const struct i2c_device_id *id) |
| 138 | { |
| 139 | struct iio_dev *indio_dev; |
| 140 | struct st_sensor_data *adata; |
Shrirang Bagul | 89a2a93f | 2016-11-24 13:33:44 +0800 | [diff] [blame] | 141 | int ret; |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 142 | |
Sachin Kamat | 6b7e0a9 | 2013-07-23 07:47:00 +0100 | [diff] [blame] | 143 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adata)); |
| 144 | if (!indio_dev) |
| 145 | return -ENOMEM; |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 146 | |
| 147 | adata = iio_priv(indio_dev); |
Shrirang Bagul | 89a2a93f | 2016-11-24 13:33:44 +0800 | [diff] [blame] | 148 | |
| 149 | if (client->dev.of_node) { |
Lorenzo Bianconi | 250bbbd | 2017-06-20 21:52:08 +0200 | [diff] [blame] | 150 | st_sensors_of_name_probe(&client->dev, st_accel_of_match, |
| 151 | client->name, sizeof(client->name)); |
Shrirang Bagul | 89a2a93f | 2016-11-24 13:33:44 +0800 | [diff] [blame] | 152 | } else if (ACPI_HANDLE(&client->dev)) { |
| 153 | ret = st_sensors_match_acpi_device(&client->dev); |
| 154 | if ((ret < 0) || (ret >= ST_ACCEL_MAX)) |
| 155 | return -ENODEV; |
| 156 | |
| 157 | strncpy(client->name, st_accel_id_table[ret].name, |
| 158 | sizeof(client->name)); |
| 159 | client->name[sizeof(client->name) - 1] = '\0'; |
| 160 | } else if (!id) |
| 161 | return -ENODEV; |
| 162 | |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 163 | |
| 164 | st_sensors_i2c_configure(indio_dev, client, adata); |
| 165 | |
Shrirang Bagul | 89a2a93f | 2016-11-24 13:33:44 +0800 | [diff] [blame] | 166 | ret = st_accel_common_probe(indio_dev); |
| 167 | if (ret < 0) |
| 168 | return ret; |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 169 | |
| 170 | return 0; |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static int st_accel_i2c_remove(struct i2c_client *client) |
| 174 | { |
| 175 | st_accel_common_remove(i2c_get_clientdata(client)); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 180 | static struct i2c_driver st_accel_driver = { |
| 181 | .driver = { |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 182 | .name = "st-accel-i2c", |
Linus Walleij | 2d7768a | 2014-08-07 08:16:00 +0100 | [diff] [blame] | 183 | .of_match_table = of_match_ptr(st_accel_of_match), |
Shrirang Bagul | 89a2a93f | 2016-11-24 13:33:44 +0800 | [diff] [blame] | 184 | .acpi_match_table = ACPI_PTR(st_accel_acpi_match), |
Denis Ciocca | d625116 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 185 | }, |
| 186 | .probe = st_accel_i2c_probe, |
| 187 | .remove = st_accel_i2c_remove, |
| 188 | .id_table = st_accel_id_table, |
| 189 | }; |
| 190 | module_i2c_driver(st_accel_driver); |
| 191 | |
| 192 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); |
| 193 | MODULE_DESCRIPTION("STMicroelectronics accelerometers i2c driver"); |
| 194 | MODULE_LICENSE("GPL v2"); |