18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/** 38c2ecf20Sopenharmony_ci * Sensortek STK8BA50 3-Axis Accelerometer 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2015, Intel Corporation. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * STK8BA50 7-bit I2C address: 0x18. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/acpi.h> 118c2ecf20Sopenharmony_ci#include <linux/i2c.h> 128c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 138c2ecf20Sopenharmony_ci#include <linux/kernel.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h> 168c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 178c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 188c2ecf20Sopenharmony_ci#include <linux/iio/trigger.h> 198c2ecf20Sopenharmony_ci#include <linux/iio/triggered_buffer.h> 208c2ecf20Sopenharmony_ci#include <linux/iio/trigger_consumer.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define STK8BA50_REG_XOUT 0x02 238c2ecf20Sopenharmony_ci#define STK8BA50_REG_YOUT 0x04 248c2ecf20Sopenharmony_ci#define STK8BA50_REG_ZOUT 0x06 258c2ecf20Sopenharmony_ci#define STK8BA50_REG_RANGE 0x0F 268c2ecf20Sopenharmony_ci#define STK8BA50_REG_BWSEL 0x10 278c2ecf20Sopenharmony_ci#define STK8BA50_REG_POWMODE 0x11 288c2ecf20Sopenharmony_ci#define STK8BA50_REG_SWRST 0x14 298c2ecf20Sopenharmony_ci#define STK8BA50_REG_INTEN2 0x17 308c2ecf20Sopenharmony_ci#define STK8BA50_REG_INTMAP2 0x1A 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define STK8BA50_MODE_NORMAL 0 338c2ecf20Sopenharmony_ci#define STK8BA50_MODE_SUSPEND 1 348c2ecf20Sopenharmony_ci#define STK8BA50_MODE_POWERBIT BIT(7) 358c2ecf20Sopenharmony_ci#define STK8BA50_DATA_SHIFT 6 368c2ecf20Sopenharmony_ci#define STK8BA50_RESET_CMD 0xB6 378c2ecf20Sopenharmony_ci#define STK8BA50_SR_1792HZ_IDX 7 388c2ecf20Sopenharmony_ci#define STK8BA50_DREADY_INT_MASK 0x10 398c2ecf20Sopenharmony_ci#define STK8BA50_DREADY_INT_MAP 0x81 408c2ecf20Sopenharmony_ci#define STK8BA50_ALL_CHANNEL_MASK 7 418c2ecf20Sopenharmony_ci#define STK8BA50_ALL_CHANNEL_SIZE 6 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci#define STK8BA50_DRIVER_NAME "stk8ba50" 448c2ecf20Sopenharmony_ci#define STK8BA50_IRQ_NAME "stk8ba50_event" 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci#define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069" 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci/* 498c2ecf20Sopenharmony_ci * The accelerometer has four measurement ranges: 508c2ecf20Sopenharmony_ci * +/-2g; +/-4g; +/-8g; +/-16g 518c2ecf20Sopenharmony_ci * 528c2ecf20Sopenharmony_ci * Acceleration values are 10-bit, 2's complement. 538c2ecf20Sopenharmony_ci * Scales are calculated as following: 548c2ecf20Sopenharmony_ci * 558c2ecf20Sopenharmony_ci * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384 568c2ecf20Sopenharmony_ci * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767 578c2ecf20Sopenharmony_ci * etc. 588c2ecf20Sopenharmony_ci * 598c2ecf20Sopenharmony_ci * Scales are stored in this format: 608c2ecf20Sopenharmony_ci * { <register value>, <scale value> } 618c2ecf20Sopenharmony_ci * 628c2ecf20Sopenharmony_ci * Locally, the range is stored as a table index. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_cistatic const struct { 658c2ecf20Sopenharmony_ci u8 reg_val; 668c2ecf20Sopenharmony_ci u32 scale_val; 678c2ecf20Sopenharmony_ci} stk8ba50_scale_table[] = { 688c2ecf20Sopenharmony_ci {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900} 698c2ecf20Sopenharmony_ci}; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/* Sample rates are stored as { <register value>, <Hz value> } */ 728c2ecf20Sopenharmony_cistatic const struct { 738c2ecf20Sopenharmony_ci u8 reg_val; 748c2ecf20Sopenharmony_ci u16 samp_freq; 758c2ecf20Sopenharmony_ci} stk8ba50_samp_freq_table[] = { 768c2ecf20Sopenharmony_ci {0x08, 14}, {0x09, 25}, {0x0A, 56}, {0x0B, 112}, 778c2ecf20Sopenharmony_ci {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792} 788c2ecf20Sopenharmony_ci}; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci/* Used to map scan mask bits to their corresponding channel register. */ 818c2ecf20Sopenharmony_cistatic const int stk8ba50_channel_table[] = { 828c2ecf20Sopenharmony_ci STK8BA50_REG_XOUT, 838c2ecf20Sopenharmony_ci STK8BA50_REG_YOUT, 848c2ecf20Sopenharmony_ci STK8BA50_REG_ZOUT 858c2ecf20Sopenharmony_ci}; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistruct stk8ba50_data { 888c2ecf20Sopenharmony_ci struct i2c_client *client; 898c2ecf20Sopenharmony_ci struct mutex lock; 908c2ecf20Sopenharmony_ci int range; 918c2ecf20Sopenharmony_ci u8 sample_rate_idx; 928c2ecf20Sopenharmony_ci struct iio_trigger *dready_trig; 938c2ecf20Sopenharmony_ci bool dready_trigger_on; 948c2ecf20Sopenharmony_ci /* Ensure timestamp is naturally aligned */ 958c2ecf20Sopenharmony_ci struct { 968c2ecf20Sopenharmony_ci s16 chans[3]; 978c2ecf20Sopenharmony_ci s64 timetamp __aligned(8); 988c2ecf20Sopenharmony_ci } scan; 998c2ecf20Sopenharmony_ci}; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci#define STK8BA50_ACCEL_CHANNEL(index, reg, axis) { \ 1028c2ecf20Sopenharmony_ci .type = IIO_ACCEL, \ 1038c2ecf20Sopenharmony_ci .address = reg, \ 1048c2ecf20Sopenharmony_ci .modified = 1, \ 1058c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_##axis, \ 1068c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 1078c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 1088c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 1098c2ecf20Sopenharmony_ci .scan_index = index, \ 1108c2ecf20Sopenharmony_ci .scan_type = { \ 1118c2ecf20Sopenharmony_ci .sign = 's', \ 1128c2ecf20Sopenharmony_ci .realbits = 10, \ 1138c2ecf20Sopenharmony_ci .storagebits = 16, \ 1148c2ecf20Sopenharmony_ci .shift = STK8BA50_DATA_SHIFT, \ 1158c2ecf20Sopenharmony_ci .endianness = IIO_CPU, \ 1168c2ecf20Sopenharmony_ci }, \ 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic const struct iio_chan_spec stk8ba50_channels[] = { 1208c2ecf20Sopenharmony_ci STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT, X), 1218c2ecf20Sopenharmony_ci STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT, Y), 1228c2ecf20Sopenharmony_ci STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT, Z), 1238c2ecf20Sopenharmony_ci IIO_CHAN_SOFT_TIMESTAMP(3), 1248c2ecf20Sopenharmony_ci}; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792"); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic struct attribute *stk8ba50_attributes[] = { 1318c2ecf20Sopenharmony_ci &iio_const_attr_in_accel_scale_available.dev_attr.attr, 1328c2ecf20Sopenharmony_ci &iio_const_attr_sampling_frequency_available.dev_attr.attr, 1338c2ecf20Sopenharmony_ci NULL, 1348c2ecf20Sopenharmony_ci}; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic const struct attribute_group stk8ba50_attribute_group = { 1378c2ecf20Sopenharmony_ci .attrs = stk8ba50_attributes 1388c2ecf20Sopenharmony_ci}; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci int ret; 1438c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci ret = i2c_smbus_read_word_data(client, reg); 1468c2ecf20Sopenharmony_ci if (ret < 0) { 1478c2ecf20Sopenharmony_ci dev_err(&client->dev, "register read failed\n"); 1488c2ecf20Sopenharmony_ci return ret; 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci return ret; 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_cistatic int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger *trig, 1558c2ecf20Sopenharmony_ci bool state) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1588c2ecf20Sopenharmony_ci struct stk8ba50_data *data = iio_priv(indio_dev); 1598c2ecf20Sopenharmony_ci int ret; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci if (state) 1628c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(data->client, 1638c2ecf20Sopenharmony_ci STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK); 1648c2ecf20Sopenharmony_ci else 1658c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(data->client, 1668c2ecf20Sopenharmony_ci STK8BA50_REG_INTEN2, 0x00); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (ret < 0) 1698c2ecf20Sopenharmony_ci dev_err(&data->client->dev, "failed to set trigger state\n"); 1708c2ecf20Sopenharmony_ci else 1718c2ecf20Sopenharmony_ci data->dready_trigger_on = state; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci return ret; 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic const struct iio_trigger_ops stk8ba50_trigger_ops = { 1778c2ecf20Sopenharmony_ci .set_trigger_state = stk8ba50_data_rdy_trigger_set_state, 1788c2ecf20Sopenharmony_ci}; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic int stk8ba50_set_power(struct stk8ba50_data *data, bool mode) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci int ret; 1838c2ecf20Sopenharmony_ci u8 masked_reg; 1848c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE); 1878c2ecf20Sopenharmony_ci if (ret < 0) 1888c2ecf20Sopenharmony_ci goto exit_err; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci if (mode) 1918c2ecf20Sopenharmony_ci masked_reg = ret | STK8BA50_MODE_POWERBIT; 1928c2ecf20Sopenharmony_ci else 1938c2ecf20Sopenharmony_ci masked_reg = ret & (~STK8BA50_MODE_POWERBIT); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE, 1968c2ecf20Sopenharmony_ci masked_reg); 1978c2ecf20Sopenharmony_ci if (ret < 0) 1988c2ecf20Sopenharmony_ci goto exit_err; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci return ret; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ciexit_err: 2038c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to change sensor mode\n"); 2048c2ecf20Sopenharmony_ci return ret; 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cistatic int stk8ba50_read_raw(struct iio_dev *indio_dev, 2088c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 2098c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci struct stk8ba50_data *data = iio_priv(indio_dev); 2128c2ecf20Sopenharmony_ci int ret; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci switch (mask) { 2158c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 2168c2ecf20Sopenharmony_ci if (iio_buffer_enabled(indio_dev)) 2178c2ecf20Sopenharmony_ci return -EBUSY; 2188c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 2198c2ecf20Sopenharmony_ci ret = stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); 2208c2ecf20Sopenharmony_ci if (ret < 0) { 2218c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 2228c2ecf20Sopenharmony_ci return -EINVAL; 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci ret = stk8ba50_read_accel(data, chan->address); 2258c2ecf20Sopenharmony_ci if (ret < 0) { 2268c2ecf20Sopenharmony_ci stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 2278c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 2288c2ecf20Sopenharmony_ci return -EINVAL; 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci *val = sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9); 2318c2ecf20Sopenharmony_ci stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 2328c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 2338c2ecf20Sopenharmony_ci return IIO_VAL_INT; 2348c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 2358c2ecf20Sopenharmony_ci *val = 0; 2368c2ecf20Sopenharmony_ci *val2 = stk8ba50_scale_table[data->range].scale_val; 2378c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 2388c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 2398c2ecf20Sopenharmony_ci *val = stk8ba50_samp_freq_table 2408c2ecf20Sopenharmony_ci [data->sample_rate_idx].samp_freq; 2418c2ecf20Sopenharmony_ci *val2 = 0; 2428c2ecf20Sopenharmony_ci return IIO_VAL_INT; 2438c2ecf20Sopenharmony_ci } 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci return -EINVAL; 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic int stk8ba50_write_raw(struct iio_dev *indio_dev, 2498c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 2508c2ecf20Sopenharmony_ci int val, int val2, long mask) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci int ret; 2538c2ecf20Sopenharmony_ci int i; 2548c2ecf20Sopenharmony_ci int index = -1; 2558c2ecf20Sopenharmony_ci struct stk8ba50_data *data = iio_priv(indio_dev); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci switch (mask) { 2588c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 2598c2ecf20Sopenharmony_ci if (val != 0) 2608c2ecf20Sopenharmony_ci return -EINVAL; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++) 2638c2ecf20Sopenharmony_ci if (val2 == stk8ba50_scale_table[i].scale_val) { 2648c2ecf20Sopenharmony_ci index = i; 2658c2ecf20Sopenharmony_ci break; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci if (index < 0) 2688c2ecf20Sopenharmony_ci return -EINVAL; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(data->client, 2718c2ecf20Sopenharmony_ci STK8BA50_REG_RANGE, 2728c2ecf20Sopenharmony_ci stk8ba50_scale_table[index].reg_val); 2738c2ecf20Sopenharmony_ci if (ret < 0) 2748c2ecf20Sopenharmony_ci dev_err(&data->client->dev, 2758c2ecf20Sopenharmony_ci "failed to set measurement range\n"); 2768c2ecf20Sopenharmony_ci else 2778c2ecf20Sopenharmony_ci data->range = index; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci return ret; 2808c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 2818c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++) 2828c2ecf20Sopenharmony_ci if (val == stk8ba50_samp_freq_table[i].samp_freq) { 2838c2ecf20Sopenharmony_ci index = i; 2848c2ecf20Sopenharmony_ci break; 2858c2ecf20Sopenharmony_ci } 2868c2ecf20Sopenharmony_ci if (index < 0) 2878c2ecf20Sopenharmony_ci return -EINVAL; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(data->client, 2908c2ecf20Sopenharmony_ci STK8BA50_REG_BWSEL, 2918c2ecf20Sopenharmony_ci stk8ba50_samp_freq_table[index].reg_val); 2928c2ecf20Sopenharmony_ci if (ret < 0) 2938c2ecf20Sopenharmony_ci dev_err(&data->client->dev, 2948c2ecf20Sopenharmony_ci "failed to set sampling rate\n"); 2958c2ecf20Sopenharmony_ci else 2968c2ecf20Sopenharmony_ci data->sample_rate_idx = index; 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci return ret; 2998c2ecf20Sopenharmony_ci } 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci return -EINVAL; 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_cistatic const struct iio_info stk8ba50_info = { 3058c2ecf20Sopenharmony_ci .read_raw = stk8ba50_read_raw, 3068c2ecf20Sopenharmony_ci .write_raw = stk8ba50_write_raw, 3078c2ecf20Sopenharmony_ci .attrs = &stk8ba50_attribute_group, 3088c2ecf20Sopenharmony_ci}; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic irqreturn_t stk8ba50_trigger_handler(int irq, void *p) 3118c2ecf20Sopenharmony_ci{ 3128c2ecf20Sopenharmony_ci struct iio_poll_func *pf = p; 3138c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = pf->indio_dev; 3148c2ecf20Sopenharmony_ci struct stk8ba50_data *data = iio_priv(indio_dev); 3158c2ecf20Sopenharmony_ci int bit, ret, i = 0; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3188c2ecf20Sopenharmony_ci /* 3198c2ecf20Sopenharmony_ci * Do a bulk read if all channels are requested, 3208c2ecf20Sopenharmony_ci * from 0x02 (XOUT1) to 0x07 (ZOUT2) 3218c2ecf20Sopenharmony_ci */ 3228c2ecf20Sopenharmony_ci if (*(indio_dev->active_scan_mask) == STK8BA50_ALL_CHANNEL_MASK) { 3238c2ecf20Sopenharmony_ci ret = i2c_smbus_read_i2c_block_data(data->client, 3248c2ecf20Sopenharmony_ci STK8BA50_REG_XOUT, 3258c2ecf20Sopenharmony_ci STK8BA50_ALL_CHANNEL_SIZE, 3268c2ecf20Sopenharmony_ci (u8 *)data->scan.chans); 3278c2ecf20Sopenharmony_ci if (ret < STK8BA50_ALL_CHANNEL_SIZE) { 3288c2ecf20Sopenharmony_ci dev_err(&data->client->dev, "register read failed\n"); 3298c2ecf20Sopenharmony_ci goto err; 3308c2ecf20Sopenharmony_ci } 3318c2ecf20Sopenharmony_ci } else { 3328c2ecf20Sopenharmony_ci for_each_set_bit(bit, indio_dev->active_scan_mask, 3338c2ecf20Sopenharmony_ci indio_dev->masklength) { 3348c2ecf20Sopenharmony_ci ret = stk8ba50_read_accel(data, 3358c2ecf20Sopenharmony_ci stk8ba50_channel_table[bit]); 3368c2ecf20Sopenharmony_ci if (ret < 0) 3378c2ecf20Sopenharmony_ci goto err; 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci data->scan.chans[i++] = ret; 3408c2ecf20Sopenharmony_ci } 3418c2ecf20Sopenharmony_ci } 3428c2ecf20Sopenharmony_ci iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, 3438c2ecf20Sopenharmony_ci pf->timestamp); 3448c2ecf20Sopenharmony_cierr: 3458c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3468c2ecf20Sopenharmony_ci iio_trigger_notify_done(indio_dev->trig); 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci return IRQ_HANDLED; 3498c2ecf20Sopenharmony_ci} 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_cistatic irqreturn_t stk8ba50_data_rdy_trig_poll(int irq, void *private) 3528c2ecf20Sopenharmony_ci{ 3538c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = private; 3548c2ecf20Sopenharmony_ci struct stk8ba50_data *data = iio_priv(indio_dev); 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci if (data->dready_trigger_on) 3578c2ecf20Sopenharmony_ci iio_trigger_poll(data->dready_trig); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci return IRQ_HANDLED; 3608c2ecf20Sopenharmony_ci} 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_cistatic int stk8ba50_buffer_preenable(struct iio_dev *indio_dev) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci struct stk8ba50_data *data = iio_priv(indio_dev); 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); 3678c2ecf20Sopenharmony_ci} 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_cistatic int stk8ba50_buffer_postdisable(struct iio_dev *indio_dev) 3708c2ecf20Sopenharmony_ci{ 3718c2ecf20Sopenharmony_ci struct stk8ba50_data *data = iio_priv(indio_dev); 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 3748c2ecf20Sopenharmony_ci} 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_cistatic const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops = { 3778c2ecf20Sopenharmony_ci .preenable = stk8ba50_buffer_preenable, 3788c2ecf20Sopenharmony_ci .postdisable = stk8ba50_buffer_postdisable, 3798c2ecf20Sopenharmony_ci}; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_cistatic int stk8ba50_probe(struct i2c_client *client, 3828c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci int ret; 3858c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 3868c2ecf20Sopenharmony_ci struct stk8ba50_data *data; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 3898c2ecf20Sopenharmony_ci if (!indio_dev) { 3908c2ecf20Sopenharmony_ci dev_err(&client->dev, "iio allocation failed!\n"); 3918c2ecf20Sopenharmony_ci return -ENOMEM; 3928c2ecf20Sopenharmony_ci } 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci data = iio_priv(indio_dev); 3958c2ecf20Sopenharmony_ci data->client = client; 3968c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 3978c2ecf20Sopenharmony_ci mutex_init(&data->lock); 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci indio_dev->info = &stk8ba50_info; 4008c2ecf20Sopenharmony_ci indio_dev->name = STK8BA50_DRIVER_NAME; 4018c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 4028c2ecf20Sopenharmony_ci indio_dev->channels = stk8ba50_channels; 4038c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels); 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci /* Reset all registers on startup */ 4068c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, 4078c2ecf20Sopenharmony_ci STK8BA50_REG_SWRST, STK8BA50_RESET_CMD); 4088c2ecf20Sopenharmony_ci if (ret < 0) { 4098c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to reset sensor\n"); 4108c2ecf20Sopenharmony_ci goto err_power_off; 4118c2ecf20Sopenharmony_ci } 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci /* The default range is +/-2g */ 4148c2ecf20Sopenharmony_ci data->range = 0; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci /* The default sampling rate is 1792 Hz (maximum) */ 4178c2ecf20Sopenharmony_ci data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX; 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci /* Set up interrupts */ 4208c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, 4218c2ecf20Sopenharmony_ci STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK); 4228c2ecf20Sopenharmony_ci if (ret < 0) { 4238c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to set up interrupts\n"); 4248c2ecf20Sopenharmony_ci goto err_power_off; 4258c2ecf20Sopenharmony_ci } 4268c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, 4278c2ecf20Sopenharmony_ci STK8BA50_REG_INTMAP2, STK8BA50_DREADY_INT_MAP); 4288c2ecf20Sopenharmony_ci if (ret < 0) { 4298c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to set up interrupts\n"); 4308c2ecf20Sopenharmony_ci goto err_power_off; 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci if (client->irq > 0) { 4348c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(&client->dev, client->irq, 4358c2ecf20Sopenharmony_ci stk8ba50_data_rdy_trig_poll, 4368c2ecf20Sopenharmony_ci NULL, 4378c2ecf20Sopenharmony_ci IRQF_TRIGGER_RISING | 4388c2ecf20Sopenharmony_ci IRQF_ONESHOT, 4398c2ecf20Sopenharmony_ci STK8BA50_IRQ_NAME, 4408c2ecf20Sopenharmony_ci indio_dev); 4418c2ecf20Sopenharmony_ci if (ret < 0) { 4428c2ecf20Sopenharmony_ci dev_err(&client->dev, "request irq %d failed\n", 4438c2ecf20Sopenharmony_ci client->irq); 4448c2ecf20Sopenharmony_ci goto err_power_off; 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci data->dready_trig = devm_iio_trigger_alloc(&client->dev, 4488c2ecf20Sopenharmony_ci "%s-dev%d", 4498c2ecf20Sopenharmony_ci indio_dev->name, 4508c2ecf20Sopenharmony_ci indio_dev->id); 4518c2ecf20Sopenharmony_ci if (!data->dready_trig) { 4528c2ecf20Sopenharmony_ci ret = -ENOMEM; 4538c2ecf20Sopenharmony_ci goto err_power_off; 4548c2ecf20Sopenharmony_ci } 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci data->dready_trig->dev.parent = &client->dev; 4578c2ecf20Sopenharmony_ci data->dready_trig->ops = &stk8ba50_trigger_ops; 4588c2ecf20Sopenharmony_ci iio_trigger_set_drvdata(data->dready_trig, indio_dev); 4598c2ecf20Sopenharmony_ci ret = iio_trigger_register(data->dready_trig); 4608c2ecf20Sopenharmony_ci if (ret) { 4618c2ecf20Sopenharmony_ci dev_err(&client->dev, "iio trigger register failed\n"); 4628c2ecf20Sopenharmony_ci goto err_power_off; 4638c2ecf20Sopenharmony_ci } 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci ret = iio_triggered_buffer_setup(indio_dev, 4678c2ecf20Sopenharmony_ci iio_pollfunc_store_time, 4688c2ecf20Sopenharmony_ci stk8ba50_trigger_handler, 4698c2ecf20Sopenharmony_ci &stk8ba50_buffer_setup_ops); 4708c2ecf20Sopenharmony_ci if (ret < 0) { 4718c2ecf20Sopenharmony_ci dev_err(&client->dev, "iio triggered buffer setup failed\n"); 4728c2ecf20Sopenharmony_ci goto err_trigger_unregister; 4738c2ecf20Sopenharmony_ci } 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci ret = iio_device_register(indio_dev); 4768c2ecf20Sopenharmony_ci if (ret < 0) { 4778c2ecf20Sopenharmony_ci dev_err(&client->dev, "device_register failed\n"); 4788c2ecf20Sopenharmony_ci goto err_buffer_cleanup; 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci return ret; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_cierr_buffer_cleanup: 4848c2ecf20Sopenharmony_ci iio_triggered_buffer_cleanup(indio_dev); 4858c2ecf20Sopenharmony_cierr_trigger_unregister: 4868c2ecf20Sopenharmony_ci if (data->dready_trig) 4878c2ecf20Sopenharmony_ci iio_trigger_unregister(data->dready_trig); 4888c2ecf20Sopenharmony_cierr_power_off: 4898c2ecf20Sopenharmony_ci stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 4908c2ecf20Sopenharmony_ci return ret; 4918c2ecf20Sopenharmony_ci} 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_cistatic int stk8ba50_remove(struct i2c_client *client) 4948c2ecf20Sopenharmony_ci{ 4958c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(client); 4968c2ecf20Sopenharmony_ci struct stk8ba50_data *data = iio_priv(indio_dev); 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 4998c2ecf20Sopenharmony_ci iio_triggered_buffer_cleanup(indio_dev); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci if (data->dready_trig) 5028c2ecf20Sopenharmony_ci iio_trigger_unregister(data->dready_trig); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 5058c2ecf20Sopenharmony_ci} 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 5088c2ecf20Sopenharmony_cistatic int stk8ba50_suspend(struct device *dev) 5098c2ecf20Sopenharmony_ci{ 5108c2ecf20Sopenharmony_ci struct stk8ba50_data *data; 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 5158c2ecf20Sopenharmony_ci} 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_cistatic int stk8ba50_resume(struct device *dev) 5188c2ecf20Sopenharmony_ci{ 5198c2ecf20Sopenharmony_ci struct stk8ba50_data *data; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); 5248c2ecf20Sopenharmony_ci} 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend, stk8ba50_resume); 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci#define STK8BA50_PM_OPS (&stk8ba50_pm_ops) 5298c2ecf20Sopenharmony_ci#else 5308c2ecf20Sopenharmony_ci#define STK8BA50_PM_OPS NULL 5318c2ecf20Sopenharmony_ci#endif 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_cistatic const struct i2c_device_id stk8ba50_i2c_id[] = { 5348c2ecf20Sopenharmony_ci {"stk8ba50", 0}, 5358c2ecf20Sopenharmony_ci {} 5368c2ecf20Sopenharmony_ci}; 5378c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, stk8ba50_i2c_id); 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_cistatic const struct acpi_device_id stk8ba50_acpi_id[] = { 5408c2ecf20Sopenharmony_ci {"STK8BA50", 0}, 5418c2ecf20Sopenharmony_ci {} 5428c2ecf20Sopenharmony_ci}; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id); 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_cistatic struct i2c_driver stk8ba50_driver = { 5478c2ecf20Sopenharmony_ci .driver = { 5488c2ecf20Sopenharmony_ci .name = "stk8ba50", 5498c2ecf20Sopenharmony_ci .pm = STK8BA50_PM_OPS, 5508c2ecf20Sopenharmony_ci .acpi_match_table = ACPI_PTR(stk8ba50_acpi_id), 5518c2ecf20Sopenharmony_ci }, 5528c2ecf20Sopenharmony_ci .probe = stk8ba50_probe, 5538c2ecf20Sopenharmony_ci .remove = stk8ba50_remove, 5548c2ecf20Sopenharmony_ci .id_table = stk8ba50_i2c_id, 5558c2ecf20Sopenharmony_ci}; 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_cimodule_i2c_driver(stk8ba50_driver); 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); 5608c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver"); 5618c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 562