1 /* 2 * STMicroelectronics pressures driver 3 * 4 * Copyright 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> 14 #include <linux/errno.h> 15 #include <linux/types.h> 16 #include <linux/mutex.h> 17 #include <linux/interrupt.h> 18 #include <linux/i2c.h> 19 #include <linux/gpio.h> 20 #include <linux/irq.h> 21 #include <linux/delay.h> 22 #include <linux/iio/iio.h> 23 #include <linux/iio/sysfs.h> 24 #include <linux/iio/trigger.h> 25 #include <linux/iio/buffer.h> 26 #include <asm/unaligned.h> 27 28 #include <linux/iio/common/st_sensors.h> 29 #include "st_pressure.h" 30 31 #define ST_PRESS_LSB_PER_MBAR 4096UL 32 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \ 33 ST_PRESS_LSB_PER_MBAR) 34 #define ST_PRESS_LSB_PER_CELSIUS 480UL 35 #define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \ 36 ST_PRESS_LSB_PER_CELSIUS) 37 #define ST_PRESS_NUMBER_DATA_CHANNELS 1 38 39 /* DEFAULT VALUE FOR SENSORS */ 40 #define ST_PRESS_DEFAULT_OUT_XL_ADDR 0x28 41 #define ST_TEMP_DEFAULT_OUT_L_ADDR 0x2b 42 43 /* FULLSCALE */ 44 #define ST_PRESS_FS_AVL_1260MB 1260 45 46 /* CUSTOM VALUES FOR SENSOR 1 */ 47 #define ST_PRESS_1_WAI_EXP 0xbb 48 #define ST_PRESS_1_ODR_ADDR 0x20 49 #define ST_PRESS_1_ODR_MASK 0x70 50 #define ST_PRESS_1_ODR_AVL_1HZ_VAL 0x01 51 #define ST_PRESS_1_ODR_AVL_7HZ_VAL 0x05 52 #define ST_PRESS_1_ODR_AVL_13HZ_VAL 0x06 53 #define ST_PRESS_1_ODR_AVL_25HZ_VAL 0x07 54 #define ST_PRESS_1_PW_ADDR 0x20 55 #define ST_PRESS_1_PW_MASK 0x80 56 #define ST_PRESS_1_FS_ADDR 0x23 57 #define ST_PRESS_1_FS_MASK 0x30 58 #define ST_PRESS_1_FS_AVL_1260_VAL 0x00 59 #define ST_PRESS_1_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE 60 #define ST_PRESS_1_FS_AVL_TEMP_GAIN ST_PRESS_CELSIUS_NANO_SCALE 61 #define ST_PRESS_1_BDU_ADDR 0x20 62 #define ST_PRESS_1_BDU_MASK 0x04 63 #define ST_PRESS_1_DRDY_IRQ_ADDR 0x22 64 #define ST_PRESS_1_DRDY_IRQ_INT1_MASK 0x04 65 #define ST_PRESS_1_DRDY_IRQ_INT2_MASK 0x20 66 #define ST_PRESS_1_MULTIREAD_BIT true 67 #define ST_PRESS_1_TEMP_OFFSET 42500 68 69 static const struct iio_chan_spec st_press_channels[] = { 70 ST_SENSORS_LSM_CHANNELS(IIO_PRESSURE, 71 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 72 ST_SENSORS_SCAN_X, 0, IIO_NO_MOD, 'u', IIO_LE, 24, 24, 73 ST_PRESS_DEFAULT_OUT_XL_ADDR), 74 ST_SENSORS_LSM_CHANNELS(IIO_TEMP, 75 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) | 76 BIT(IIO_CHAN_INFO_OFFSET), 77 -1, 0, IIO_NO_MOD, 's', IIO_LE, 16, 16, 78 ST_TEMP_DEFAULT_OUT_L_ADDR), 79 IIO_CHAN_SOFT_TIMESTAMP(1) 80 }; 81 82 static const struct st_sensors st_press_sensors[] = { 83 { 84 .wai = ST_PRESS_1_WAI_EXP, 85 .sensors_supported = { 86 [0] = LPS331AP_PRESS_DEV_NAME, 87 }, 88 .ch = (struct iio_chan_spec *)st_press_channels, 89 .odr = { 90 .addr = ST_PRESS_1_ODR_ADDR, 91 .mask = ST_PRESS_1_ODR_MASK, 92 .odr_avl = { 93 { 1, ST_PRESS_1_ODR_AVL_1HZ_VAL, }, 94 { 7, ST_PRESS_1_ODR_AVL_7HZ_VAL, }, 95 { 13, ST_PRESS_1_ODR_AVL_13HZ_VAL, }, 96 { 25, ST_PRESS_1_ODR_AVL_25HZ_VAL, }, 97 }, 98 }, 99 .pw = { 100 .addr = ST_PRESS_1_PW_ADDR, 101 .mask = ST_PRESS_1_PW_MASK, 102 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 103 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 104 }, 105 .fs = { 106 .addr = ST_PRESS_1_FS_ADDR, 107 .mask = ST_PRESS_1_FS_MASK, 108 .fs_avl = { 109 [0] = { 110 .num = ST_PRESS_FS_AVL_1260MB, 111 .value = ST_PRESS_1_FS_AVL_1260_VAL, 112 .gain = ST_PRESS_1_FS_AVL_1260_GAIN, 113 .gain2 = ST_PRESS_1_FS_AVL_TEMP_GAIN, 114 }, 115 }, 116 }, 117 .bdu = { 118 .addr = ST_PRESS_1_BDU_ADDR, 119 .mask = ST_PRESS_1_BDU_MASK, 120 }, 121 .drdy_irq = { 122 .addr = ST_PRESS_1_DRDY_IRQ_ADDR, 123 .mask_int1 = ST_PRESS_1_DRDY_IRQ_INT1_MASK, 124 .mask_int2 = ST_PRESS_1_DRDY_IRQ_INT2_MASK, 125 }, 126 .multi_read_bit = ST_PRESS_1_MULTIREAD_BIT, 127 .bootime = 2, 128 }, 129 }; 130 131 static int st_press_read_raw(struct iio_dev *indio_dev, 132 struct iio_chan_spec const *ch, int *val, 133 int *val2, long mask) 134 { 135 int err; 136 struct st_sensor_data *pdata = iio_priv(indio_dev); 137 138 switch (mask) { 139 case IIO_CHAN_INFO_RAW: 140 err = st_sensors_read_info_raw(indio_dev, ch, val); 141 if (err < 0) 142 goto read_error; 143 144 return IIO_VAL_INT; 145 case IIO_CHAN_INFO_SCALE: 146 *val = 0; 147 148 switch (ch->type) { 149 case IIO_PRESSURE: 150 *val2 = pdata->current_fullscale->gain; 151 break; 152 case IIO_TEMP: 153 *val2 = pdata->current_fullscale->gain2; 154 break; 155 default: 156 err = -EINVAL; 157 goto read_error; 158 } 159 160 return IIO_VAL_INT_PLUS_NANO; 161 case IIO_CHAN_INFO_OFFSET: 162 switch (ch->type) { 163 case IIO_TEMP: 164 *val = 425; 165 *val2 = 10; 166 break; 167 default: 168 err = -EINVAL; 169 goto read_error; 170 } 171 172 return IIO_VAL_FRACTIONAL; 173 default: 174 return -EINVAL; 175 } 176 177 read_error: 178 return err; 179 } 180 181 static ST_SENSOR_DEV_ATTR_SAMP_FREQ(); 182 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 183 184 static struct attribute *st_press_attributes[] = { 185 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 186 &iio_dev_attr_sampling_frequency.dev_attr.attr, 187 NULL, 188 }; 189 190 static const struct attribute_group st_press_attribute_group = { 191 .attrs = st_press_attributes, 192 }; 193 194 static const struct iio_info press_info = { 195 .driver_module = THIS_MODULE, 196 .attrs = &st_press_attribute_group, 197 .read_raw = &st_press_read_raw, 198 }; 199 200 #ifdef CONFIG_IIO_TRIGGER 201 static const struct iio_trigger_ops st_press_trigger_ops = { 202 .owner = THIS_MODULE, 203 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE, 204 }; 205 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops) 206 #else 207 #define ST_PRESS_TRIGGER_OPS NULL 208 #endif 209 210 int st_press_common_probe(struct iio_dev *indio_dev, 211 struct st_sensors_platform_data *plat_data) 212 { 213 int err; 214 struct st_sensor_data *pdata = iio_priv(indio_dev); 215 216 indio_dev->modes = INDIO_DIRECT_MODE; 217 indio_dev->info = &press_info; 218 219 err = st_sensors_check_device_support(indio_dev, 220 ARRAY_SIZE(st_press_sensors), st_press_sensors); 221 if (err < 0) 222 goto st_press_common_probe_error; 223 224 pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS; 225 pdata->multiread_bit = pdata->sensor->multi_read_bit; 226 indio_dev->channels = pdata->sensor->ch; 227 indio_dev->num_channels = ARRAY_SIZE(st_press_channels); 228 229 pdata->current_fullscale = (struct st_sensor_fullscale_avl *) 230 &pdata->sensor->fs.fs_avl[0]; 231 pdata->odr = pdata->sensor->odr.odr_avl[0].hz; 232 233 if (!plat_data) 234 plat_data = 235 (struct st_sensors_platform_data *)&default_press_pdata; 236 237 err = st_sensors_init_sensor(indio_dev, plat_data); 238 if (err < 0) 239 goto st_press_common_probe_error; 240 241 if (pdata->get_irq_data_ready(indio_dev) > 0) { 242 err = st_press_allocate_ring(indio_dev); 243 if (err < 0) 244 goto st_press_common_probe_error; 245 246 err = st_sensors_allocate_trigger(indio_dev, 247 ST_PRESS_TRIGGER_OPS); 248 if (err < 0) 249 goto st_press_probe_trigger_error; 250 } 251 252 err = iio_device_register(indio_dev); 253 if (err) 254 goto st_press_device_register_error; 255 256 return err; 257 258 st_press_device_register_error: 259 if (pdata->get_irq_data_ready(indio_dev) > 0) 260 st_sensors_deallocate_trigger(indio_dev); 261 st_press_probe_trigger_error: 262 if (pdata->get_irq_data_ready(indio_dev) > 0) 263 st_press_deallocate_ring(indio_dev); 264 st_press_common_probe_error: 265 return err; 266 } 267 EXPORT_SYMBOL(st_press_common_probe); 268 269 void st_press_common_remove(struct iio_dev *indio_dev) 270 { 271 struct st_sensor_data *pdata = iio_priv(indio_dev); 272 273 iio_device_unregister(indio_dev); 274 if (pdata->get_irq_data_ready(indio_dev) > 0) { 275 st_sensors_deallocate_trigger(indio_dev); 276 st_press_deallocate_ring(indio_dev); 277 } 278 } 279 EXPORT_SYMBOL(st_press_common_remove); 280 281 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 282 MODULE_DESCRIPTION("STMicroelectronics pressures driver"); 283 MODULE_LICENSE("GPL v2"); 284