Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: accel: Add sampling rate support for STK8BA50

Added support for setting the STK8BA50 accelerometer's
sampling rate.

Signed-off-by: Tiberiu Breana <tiberiu.a.breana@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Tiberiu Breana and committed by
Jonathan Cameron
eb2c9ce2 003f4880

+50 -7
+50 -7
drivers/iio/accel/stk8ba50.c
··· 21 21 #define STK8BA50_REG_YOUT 0x04 22 22 #define STK8BA50_REG_ZOUT 0x06 23 23 #define STK8BA50_REG_RANGE 0x0F 24 + #define STK8BA50_REG_BWSEL 0x10 24 25 #define STK8BA50_REG_POWMODE 0x11 25 26 #define STK8BA50_REG_SWRST 0x14 26 27 ··· 30 29 #define STK8BA50_MODE_POWERBIT BIT(7) 31 30 #define STK8BA50_DATA_SHIFT 6 32 31 #define STK8BA50_RESET_CMD 0xB6 32 + #define STK8BA50_SR_1792HZ_IDX 7 33 33 34 34 #define STK8BA50_DRIVER_NAME "stk8ba50" 35 35 ··· 59 57 {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900} 60 58 }; 61 59 60 + /* Sample rates are stored as { <register value>, <Hz value> } */ 61 + static const struct { 62 + u8 reg_val; 63 + u16 samp_freq; 64 + } stk8ba50_samp_freq_table[] = { 65 + {0x08, 14}, {0x09, 25}, {0x0A, 56}, {0x0B, 112}, 66 + {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792} 67 + }; 68 + 62 69 struct stk8ba50_data { 63 70 struct i2c_client *client; 64 71 struct mutex lock; 65 72 int range; 73 + u8 sample_rate_idx; 66 74 }; 67 75 68 - #define STK8BA50_ACCEL_CHANNEL(reg, axis) { \ 69 - .type = IIO_ACCEL, \ 70 - .address = reg, \ 71 - .modified = 1, \ 72 - .channel2 = IIO_MOD_##axis, \ 73 - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 74 - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 76 + #define STK8BA50_ACCEL_CHANNEL(reg, axis) { \ 77 + .type = IIO_ACCEL, \ 78 + .address = reg, \ 79 + .modified = 1, \ 80 + .channel2 = IIO_MOD_##axis, \ 81 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 82 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 83 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 75 84 } 76 85 77 86 static const struct iio_chan_spec stk8ba50_channels[] = { ··· 93 80 94 81 static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL); 95 82 83 + static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792"); 84 + 96 85 static struct attribute *stk8ba50_attributes[] = { 97 86 &iio_const_attr_in_accel_scale_available.dev_attr.attr, 87 + &iio_const_attr_sampling_frequency_available.dev_attr.attr, 98 88 NULL, 99 89 }; 100 90 ··· 135 119 *val = 0; 136 120 *val2 = stk8ba50_scale_table[data->range].scale_val; 137 121 return IIO_VAL_INT_PLUS_MICRO; 122 + case IIO_CHAN_INFO_SAMP_FREQ: 123 + *val = stk8ba50_samp_freq_table 124 + [data->sample_rate_idx].samp_freq; 125 + *val2 = 0; 126 + return IIO_VAL_INT; 138 127 } 139 128 140 129 return -EINVAL; ··· 175 154 "failed to set measurement range\n"); 176 155 else 177 156 data->range = index; 157 + 158 + return ret; 159 + case IIO_CHAN_INFO_SAMP_FREQ: 160 + for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++) 161 + if (val == stk8ba50_samp_freq_table[i].samp_freq) { 162 + index = i; 163 + break; 164 + } 165 + if (index < 0) 166 + return -EINVAL; 167 + 168 + ret = i2c_smbus_write_byte_data(data->client, 169 + STK8BA50_REG_BWSEL, 170 + stk8ba50_samp_freq_table[index].reg_val); 171 + if (ret < 0) 172 + dev_err(&data->client->dev, 173 + "failed to set sampling rate\n"); 174 + else 175 + data->sample_rate_idx = index; 178 176 179 177 return ret; 180 178 } ··· 270 230 271 231 /* The default range is +/-2g */ 272 232 data->range = 0; 233 + 234 + /* The default sampling rate is 1792 Hz (maximum) */ 235 + data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX; 273 236 274 237 ret = iio_device_register(indio_dev); 275 238 if (ret < 0) {