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