1d6ad8058SMatt Ranostay // SPDX-License-Identifier: GPL-2.0+ 2aff268cdSMatt Ranostay /* 3aff268cdSMatt Ranostay * apds9960.c - Support for Avago APDS9960 gesture/RGB/ALS/proximity sensor 4aff268cdSMatt Ranostay * 5d6ad8058SMatt Ranostay * Copyright (C) 2015, 2018 6d6ad8058SMatt Ranostay * Author: Matt Ranostay <matt.ranostay@konsulko.com> 7aff268cdSMatt Ranostay * 8aff268cdSMatt Ranostay * TODO: gesture + proximity calib offsets 9aff268cdSMatt Ranostay */ 10aff268cdSMatt Ranostay 11aff268cdSMatt Ranostay #include <linux/module.h> 12aff268cdSMatt Ranostay #include <linux/init.h> 13aff268cdSMatt Ranostay #include <linux/interrupt.h> 14aff268cdSMatt Ranostay #include <linux/delay.h> 15aff268cdSMatt Ranostay #include <linux/mutex.h> 16aff268cdSMatt Ranostay #include <linux/err.h> 17aff268cdSMatt Ranostay #include <linux/irq.h> 18aff268cdSMatt Ranostay #include <linux/i2c.h> 19aff268cdSMatt Ranostay #include <linux/pm_runtime.h> 20aff268cdSMatt Ranostay #include <linux/regmap.h> 21aff268cdSMatt Ranostay #include <linux/iio/iio.h> 22aff268cdSMatt Ranostay #include <linux/iio/buffer.h> 23aff268cdSMatt Ranostay #include <linux/iio/events.h> 24aff268cdSMatt Ranostay #include <linux/iio/kfifo_buf.h> 25aff268cdSMatt Ranostay #include <linux/iio/sysfs.h> 26aff268cdSMatt Ranostay 27aff268cdSMatt Ranostay #define APDS9960_REGMAP_NAME "apds9960_regmap" 28aff268cdSMatt Ranostay #define APDS9960_DRV_NAME "apds9960" 29aff268cdSMatt Ranostay 30aff268cdSMatt Ranostay #define APDS9960_REG_RAM_START 0x00 31aff268cdSMatt Ranostay #define APDS9960_REG_RAM_END 0x7f 32aff268cdSMatt Ranostay 33aff268cdSMatt Ranostay #define APDS9960_REG_ENABLE 0x80 34aff268cdSMatt Ranostay #define APDS9960_REG_ATIME 0x81 35aff268cdSMatt Ranostay #define APDS9960_REG_WTIME 0x83 36aff268cdSMatt Ranostay 37aff268cdSMatt Ranostay #define APDS9960_REG_AILTL 0x84 38aff268cdSMatt Ranostay #define APDS9960_REG_AILTH 0x85 39aff268cdSMatt Ranostay #define APDS9960_REG_AIHTL 0x86 40aff268cdSMatt Ranostay #define APDS9960_REG_AIHTH 0x87 41aff268cdSMatt Ranostay 42aff268cdSMatt Ranostay #define APDS9960_REG_PILT 0x89 43aff268cdSMatt Ranostay #define APDS9960_REG_PIHT 0x8b 44aff268cdSMatt Ranostay #define APDS9960_REG_PERS 0x8c 45aff268cdSMatt Ranostay 46aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_1 0x8d 47aff268cdSMatt Ranostay #define APDS9960_REG_PPULSE 0x8e 48aff268cdSMatt Ranostay 49aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL 0x8f 50aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL_AGAIN_MASK 0x03 51aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL_PGAIN_MASK 0x0c 52aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL_AGAIN_MASK_SHIFT 0 53aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT 2 54aff268cdSMatt Ranostay 55aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_2 0x90 56aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_2_GGAIN_MASK 0x60 57aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT 5 58aff268cdSMatt Ranostay 59aff268cdSMatt Ranostay #define APDS9960_REG_ID 0x92 60aff268cdSMatt Ranostay 61aff268cdSMatt Ranostay #define APDS9960_REG_STATUS 0x93 62aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_PS_INT BIT(5) 63aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_ALS_INT BIT(4) 64aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_GINT BIT(2) 65aff268cdSMatt Ranostay 66aff268cdSMatt Ranostay #define APDS9960_REG_PDATA 0x9c 67aff268cdSMatt Ranostay #define APDS9960_REG_POFFSET_UR 0x9d 68aff268cdSMatt Ranostay #define APDS9960_REG_POFFSET_DL 0x9e 69aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_3 0x9f 70aff268cdSMatt Ranostay 71aff268cdSMatt Ranostay #define APDS9960_REG_GPENTH 0xa0 72aff268cdSMatt Ranostay #define APDS9960_REG_GEXTH 0xa1 73aff268cdSMatt Ranostay 74aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1 0xa2 75aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK 0xc0 76aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT 6 77aff268cdSMatt Ranostay 78aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_2 0xa3 79aff268cdSMatt Ranostay #define APDS9960_REG_GOFFSET_U 0xa4 80aff268cdSMatt Ranostay #define APDS9960_REG_GOFFSET_D 0xa5 81aff268cdSMatt Ranostay #define APDS9960_REG_GPULSE 0xa6 82aff268cdSMatt Ranostay #define APDS9960_REG_GOFFSET_L 0xa7 83aff268cdSMatt Ranostay #define APDS9960_REG_GOFFSET_R 0xa9 84aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_3 0xaa 85aff268cdSMatt Ranostay 86aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_4 0xab 87aff268cdSMatt Ranostay #define APDS9960_REG_GFLVL 0xae 88aff268cdSMatt Ranostay #define APDS9960_REG_GSTATUS 0xaf 89aff268cdSMatt Ranostay 90aff268cdSMatt Ranostay #define APDS9960_REG_IFORCE 0xe4 91aff268cdSMatt Ranostay #define APDS9960_REG_PICLEAR 0xe5 92aff268cdSMatt Ranostay #define APDS9960_REG_CICLEAR 0xe6 93aff268cdSMatt Ranostay #define APDS9960_REG_AICLEAR 0xe7 94aff268cdSMatt Ranostay 95aff268cdSMatt Ranostay #define APDS9960_DEFAULT_PERS 0x33 96aff268cdSMatt Ranostay #define APDS9960_DEFAULT_GPENTH 0x50 97aff268cdSMatt Ranostay #define APDS9960_DEFAULT_GEXTH 0x40 98aff268cdSMatt Ranostay 99aff268cdSMatt Ranostay #define APDS9960_MAX_PXS_THRES_VAL 255 100aff268cdSMatt Ranostay #define APDS9960_MAX_ALS_THRES_VAL 0xffff 101aff268cdSMatt Ranostay #define APDS9960_MAX_INT_TIME_IN_US 1000000 102aff268cdSMatt Ranostay 103aff268cdSMatt Ranostay enum apds9960_als_channel_idx { 104aff268cdSMatt Ranostay IDX_ALS_CLEAR, IDX_ALS_RED, IDX_ALS_GREEN, IDX_ALS_BLUE, 105aff268cdSMatt Ranostay }; 106aff268cdSMatt Ranostay 107aff268cdSMatt Ranostay #define APDS9960_REG_ALS_BASE 0x94 108aff268cdSMatt Ranostay #define APDS9960_REG_ALS_CHANNEL(_colour) \ 109aff268cdSMatt Ranostay (APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2)) 110aff268cdSMatt Ranostay 111aff268cdSMatt Ranostay enum apds9960_gesture_channel_idx { 112aff268cdSMatt Ranostay IDX_DIR_UP, IDX_DIR_DOWN, IDX_DIR_LEFT, IDX_DIR_RIGHT, 113aff268cdSMatt Ranostay }; 114aff268cdSMatt Ranostay 115aff268cdSMatt Ranostay #define APDS9960_REG_GFIFO_BASE 0xfc 116aff268cdSMatt Ranostay #define APDS9960_REG_GFIFO_DIR(_dir) \ 117aff268cdSMatt Ranostay (APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir) 118aff268cdSMatt Ranostay 119aff268cdSMatt Ranostay struct apds9960_data { 120aff268cdSMatt Ranostay struct i2c_client *client; 121aff268cdSMatt Ranostay struct iio_dev *indio_dev; 122aff268cdSMatt Ranostay struct mutex lock; 123aff268cdSMatt Ranostay 124aff268cdSMatt Ranostay /* regmap fields */ 125aff268cdSMatt Ranostay struct regmap *regmap; 126aff268cdSMatt Ranostay struct regmap_field *reg_int_als; 127aff268cdSMatt Ranostay struct regmap_field *reg_int_ges; 128aff268cdSMatt Ranostay struct regmap_field *reg_int_pxs; 129aff268cdSMatt Ranostay 130aff268cdSMatt Ranostay struct regmap_field *reg_enable_als; 131aff268cdSMatt Ranostay struct regmap_field *reg_enable_ges; 132aff268cdSMatt Ranostay struct regmap_field *reg_enable_pxs; 133aff268cdSMatt Ranostay 134aff268cdSMatt Ranostay /* state */ 135aff268cdSMatt Ranostay int als_int; 136aff268cdSMatt Ranostay int pxs_int; 137aff268cdSMatt Ranostay int gesture_mode_running; 138aff268cdSMatt Ranostay 139aff268cdSMatt Ranostay /* gain values */ 140aff268cdSMatt Ranostay int als_gain; 141aff268cdSMatt Ranostay int pxs_gain; 142aff268cdSMatt Ranostay 143aff268cdSMatt Ranostay /* integration time value in us */ 144aff268cdSMatt Ranostay int als_adc_int_us; 145aff268cdSMatt Ranostay 146aff268cdSMatt Ranostay /* gesture buffer */ 147aff268cdSMatt Ranostay u8 buffer[4]; /* 4 8-bit channels */ 148aff268cdSMatt Ranostay }; 149aff268cdSMatt Ranostay 150aff268cdSMatt Ranostay static const struct reg_default apds9960_reg_defaults[] = { 151aff268cdSMatt Ranostay /* Default ALS integration time = 2.48ms */ 152aff268cdSMatt Ranostay { APDS9960_REG_ATIME, 0xff }, 153aff268cdSMatt Ranostay }; 154aff268cdSMatt Ranostay 155aff268cdSMatt Ranostay static const struct regmap_range apds9960_volatile_ranges[] = { 156aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_STATUS, 157aff268cdSMatt Ranostay APDS9960_REG_PDATA), 158aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_GFLVL, 159aff268cdSMatt Ranostay APDS9960_REG_GSTATUS), 160aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP), 161aff268cdSMatt Ranostay APDS9960_REG_GFIFO_DIR(RIGHT)), 162aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_IFORCE, 163aff268cdSMatt Ranostay APDS9960_REG_AICLEAR), 164aff268cdSMatt Ranostay }; 165aff268cdSMatt Ranostay 166aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_volatile_table = { 167aff268cdSMatt Ranostay .yes_ranges = apds9960_volatile_ranges, 168aff268cdSMatt Ranostay .n_yes_ranges = ARRAY_SIZE(apds9960_volatile_ranges), 169aff268cdSMatt Ranostay }; 170aff268cdSMatt Ranostay 171aff268cdSMatt Ranostay static const struct regmap_range apds9960_precious_ranges[] = { 172aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END), 173aff268cdSMatt Ranostay }; 174aff268cdSMatt Ranostay 175aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_precious_table = { 176aff268cdSMatt Ranostay .yes_ranges = apds9960_precious_ranges, 177aff268cdSMatt Ranostay .n_yes_ranges = ARRAY_SIZE(apds9960_precious_ranges), 178aff268cdSMatt Ranostay }; 179aff268cdSMatt Ranostay 180aff268cdSMatt Ranostay static const struct regmap_range apds9960_readable_ranges[] = { 181aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_ENABLE, 182aff268cdSMatt Ranostay APDS9960_REG_GSTATUS), 183aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP), 184aff268cdSMatt Ranostay APDS9960_REG_GFIFO_DIR(RIGHT)), 185aff268cdSMatt Ranostay }; 186aff268cdSMatt Ranostay 187aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_readable_table = { 188aff268cdSMatt Ranostay .yes_ranges = apds9960_readable_ranges, 189aff268cdSMatt Ranostay .n_yes_ranges = ARRAY_SIZE(apds9960_readable_ranges), 190aff268cdSMatt Ranostay }; 191aff268cdSMatt Ranostay 192aff268cdSMatt Ranostay static const struct regmap_range apds9960_writeable_ranges[] = { 193aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_ENABLE, APDS9960_REG_CONFIG_2), 194aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_POFFSET_UR, APDS9960_REG_GCONF_4), 195aff268cdSMatt Ranostay regmap_reg_range(APDS9960_REG_IFORCE, APDS9960_REG_AICLEAR), 196aff268cdSMatt Ranostay }; 197aff268cdSMatt Ranostay 198aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_writeable_table = { 199aff268cdSMatt Ranostay .yes_ranges = apds9960_writeable_ranges, 200aff268cdSMatt Ranostay .n_yes_ranges = ARRAY_SIZE(apds9960_writeable_ranges), 201aff268cdSMatt Ranostay }; 202aff268cdSMatt Ranostay 203aff268cdSMatt Ranostay static const struct regmap_config apds9960_regmap_config = { 204aff268cdSMatt Ranostay .name = APDS9960_REGMAP_NAME, 205aff268cdSMatt Ranostay .reg_bits = 8, 206aff268cdSMatt Ranostay .val_bits = 8, 2071c96a2f6SDavid Frey .use_single_read = true, 2081c96a2f6SDavid Frey .use_single_write = true, 209aff268cdSMatt Ranostay 210aff268cdSMatt Ranostay .volatile_table = &apds9960_volatile_table, 211aff268cdSMatt Ranostay .precious_table = &apds9960_precious_table, 212aff268cdSMatt Ranostay .rd_table = &apds9960_readable_table, 213aff268cdSMatt Ranostay .wr_table = &apds9960_writeable_table, 214aff268cdSMatt Ranostay 215aff268cdSMatt Ranostay .reg_defaults = apds9960_reg_defaults, 216aff268cdSMatt Ranostay .num_reg_defaults = ARRAY_SIZE(apds9960_reg_defaults), 217aff268cdSMatt Ranostay .max_register = APDS9960_REG_GFIFO_DIR(RIGHT), 218aff268cdSMatt Ranostay .cache_type = REGCACHE_RBTREE, 219aff268cdSMatt Ranostay }; 220aff268cdSMatt Ranostay 221aff268cdSMatt Ranostay static const struct iio_event_spec apds9960_pxs_event_spec[] = { 222aff268cdSMatt Ranostay { 223aff268cdSMatt Ranostay .type = IIO_EV_TYPE_THRESH, 224aff268cdSMatt Ranostay .dir = IIO_EV_DIR_RISING, 225aff268cdSMatt Ranostay .mask_separate = BIT(IIO_EV_INFO_VALUE) | 226aff268cdSMatt Ranostay BIT(IIO_EV_INFO_ENABLE), 227aff268cdSMatt Ranostay }, 228aff268cdSMatt Ranostay { 229aff268cdSMatt Ranostay .type = IIO_EV_TYPE_THRESH, 230aff268cdSMatt Ranostay .dir = IIO_EV_DIR_FALLING, 231aff268cdSMatt Ranostay .mask_separate = BIT(IIO_EV_INFO_VALUE) | 232aff268cdSMatt Ranostay BIT(IIO_EV_INFO_ENABLE), 233aff268cdSMatt Ranostay }, 234aff268cdSMatt Ranostay }; 235aff268cdSMatt Ranostay 236aff268cdSMatt Ranostay static const struct iio_event_spec apds9960_als_event_spec[] = { 237aff268cdSMatt Ranostay { 238aff268cdSMatt Ranostay .type = IIO_EV_TYPE_THRESH, 239aff268cdSMatt Ranostay .dir = IIO_EV_DIR_RISING, 240aff268cdSMatt Ranostay .mask_separate = BIT(IIO_EV_INFO_VALUE) | 241aff268cdSMatt Ranostay BIT(IIO_EV_INFO_ENABLE), 242aff268cdSMatt Ranostay }, 243aff268cdSMatt Ranostay { 244aff268cdSMatt Ranostay .type = IIO_EV_TYPE_THRESH, 245aff268cdSMatt Ranostay .dir = IIO_EV_DIR_FALLING, 246aff268cdSMatt Ranostay .mask_separate = BIT(IIO_EV_INFO_VALUE) | 247aff268cdSMatt Ranostay BIT(IIO_EV_INFO_ENABLE), 248aff268cdSMatt Ranostay }, 249aff268cdSMatt Ranostay }; 250aff268cdSMatt Ranostay 251aff268cdSMatt Ranostay #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \ 252aff268cdSMatt Ranostay .type = IIO_PROXIMITY, \ 253aff268cdSMatt Ranostay .channel = _si + 1, \ 254aff268cdSMatt Ranostay .scan_index = _si, \ 255aff268cdSMatt Ranostay .indexed = 1, \ 256aff268cdSMatt Ranostay .scan_type = { \ 257aff268cdSMatt Ranostay .sign = 'u', \ 258aff268cdSMatt Ranostay .realbits = 8, \ 259aff268cdSMatt Ranostay .storagebits = 8, \ 260aff268cdSMatt Ranostay }, \ 261aff268cdSMatt Ranostay } 262aff268cdSMatt Ranostay 263aff268cdSMatt Ranostay #define APDS9960_INTENSITY_CHANNEL(_colour) { \ 264aff268cdSMatt Ranostay .type = IIO_INTENSITY, \ 265aff268cdSMatt Ranostay .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 266aff268cdSMatt Ranostay .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 267aff268cdSMatt Ranostay BIT(IIO_CHAN_INFO_INT_TIME), \ 268aff268cdSMatt Ranostay .channel2 = IIO_MOD_LIGHT_##_colour, \ 269aff268cdSMatt Ranostay .address = APDS9960_REG_ALS_CHANNEL(_colour), \ 270aff268cdSMatt Ranostay .modified = 1, \ 271aff268cdSMatt Ranostay .scan_index = -1, \ 272aff268cdSMatt Ranostay } 273aff268cdSMatt Ranostay 274aff268cdSMatt Ranostay static const unsigned long apds9960_scan_masks[] = {0xf, 0}; 275aff268cdSMatt Ranostay 276aff268cdSMatt Ranostay static const struct iio_chan_spec apds9960_channels[] = { 277aff268cdSMatt Ranostay { 278aff268cdSMatt Ranostay .type = IIO_PROXIMITY, 279aff268cdSMatt Ranostay .address = APDS9960_REG_PDATA, 280aff268cdSMatt Ranostay .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 281aff268cdSMatt Ranostay .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 282aff268cdSMatt Ranostay .channel = 0, 283aff268cdSMatt Ranostay .indexed = 0, 284aff268cdSMatt Ranostay .scan_index = -1, 285aff268cdSMatt Ranostay 286aff268cdSMatt Ranostay .event_spec = apds9960_pxs_event_spec, 287aff268cdSMatt Ranostay .num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec), 288aff268cdSMatt Ranostay }, 289aff268cdSMatt Ranostay /* Gesture Sensor */ 290aff268cdSMatt Ranostay APDS9960_GESTURE_CHANNEL(UP, 0), 291aff268cdSMatt Ranostay APDS9960_GESTURE_CHANNEL(DOWN, 1), 292aff268cdSMatt Ranostay APDS9960_GESTURE_CHANNEL(LEFT, 2), 293aff268cdSMatt Ranostay APDS9960_GESTURE_CHANNEL(RIGHT, 3), 294aff268cdSMatt Ranostay /* ALS */ 295aff268cdSMatt Ranostay { 296aff268cdSMatt Ranostay .type = IIO_INTENSITY, 297aff268cdSMatt Ranostay .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 298aff268cdSMatt Ranostay .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | 299aff268cdSMatt Ranostay BIT(IIO_CHAN_INFO_INT_TIME), 300aff268cdSMatt Ranostay .channel2 = IIO_MOD_LIGHT_CLEAR, 301aff268cdSMatt Ranostay .address = APDS9960_REG_ALS_CHANNEL(CLEAR), 302aff268cdSMatt Ranostay .modified = 1, 303aff268cdSMatt Ranostay .scan_index = -1, 304aff268cdSMatt Ranostay 305aff268cdSMatt Ranostay .event_spec = apds9960_als_event_spec, 306aff268cdSMatt Ranostay .num_event_specs = ARRAY_SIZE(apds9960_als_event_spec), 307aff268cdSMatt Ranostay }, 308aff268cdSMatt Ranostay /* RGB Sensor */ 309aff268cdSMatt Ranostay APDS9960_INTENSITY_CHANNEL(RED), 310aff268cdSMatt Ranostay APDS9960_INTENSITY_CHANNEL(GREEN), 311aff268cdSMatt Ranostay APDS9960_INTENSITY_CHANNEL(BLUE), 312aff268cdSMatt Ranostay }; 313aff268cdSMatt Ranostay 314aff268cdSMatt Ranostay /* integration time in us */ 315ebe16806SSlawomir Stepien static const int apds9960_int_time[][2] = { 316ebe16806SSlawomir Stepien { 28000, 246}, 317ebe16806SSlawomir Stepien {100000, 219}, 318ebe16806SSlawomir Stepien {200000, 182}, 319ebe16806SSlawomir Stepien {700000, 0} 320ebe16806SSlawomir Stepien }; 321aff268cdSMatt Ranostay 322aff268cdSMatt Ranostay /* gain mapping */ 323aff268cdSMatt Ranostay static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8}; 324aff268cdSMatt Ranostay static const int apds9960_als_gain_map[] = {1, 4, 16, 64}; 325aff268cdSMatt Ranostay 326aff268cdSMatt Ranostay static IIO_CONST_ATTR(proximity_scale_available, "1 2 4 8"); 327aff268cdSMatt Ranostay static IIO_CONST_ATTR(intensity_scale_available, "1 4 16 64"); 328aff268cdSMatt Ranostay static IIO_CONST_ATTR_INT_TIME_AVAIL("0.028 0.1 0.2 0.7"); 329aff268cdSMatt Ranostay 330aff268cdSMatt Ranostay static struct attribute *apds9960_attributes[] = { 331aff268cdSMatt Ranostay &iio_const_attr_proximity_scale_available.dev_attr.attr, 332aff268cdSMatt Ranostay &iio_const_attr_intensity_scale_available.dev_attr.attr, 333aff268cdSMatt Ranostay &iio_const_attr_integration_time_available.dev_attr.attr, 334aff268cdSMatt Ranostay NULL, 335aff268cdSMatt Ranostay }; 336aff268cdSMatt Ranostay 3373ca437c0Ssimran singhal static const struct attribute_group apds9960_attribute_group = { 338aff268cdSMatt Ranostay .attrs = apds9960_attributes, 339aff268cdSMatt Ranostay }; 340aff268cdSMatt Ranostay 341aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_als = 342aff268cdSMatt Ranostay REG_FIELD(APDS9960_REG_ENABLE, 4, 4); 343aff268cdSMatt Ranostay 344aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_ges = 345aff268cdSMatt Ranostay REG_FIELD(APDS9960_REG_GCONF_4, 1, 1); 346aff268cdSMatt Ranostay 347aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_pxs = 348aff268cdSMatt Ranostay REG_FIELD(APDS9960_REG_ENABLE, 5, 5); 349aff268cdSMatt Ranostay 350aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_als = 351aff268cdSMatt Ranostay REG_FIELD(APDS9960_REG_ENABLE, 1, 1); 352aff268cdSMatt Ranostay 353aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_ges = 354aff268cdSMatt Ranostay REG_FIELD(APDS9960_REG_ENABLE, 6, 6); 355aff268cdSMatt Ranostay 356aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_pxs = 357aff268cdSMatt Ranostay REG_FIELD(APDS9960_REG_ENABLE, 2, 2); 358aff268cdSMatt Ranostay 359aff268cdSMatt Ranostay static int apds9960_set_it_time(struct apds9960_data *data, int val2) 360aff268cdSMatt Ranostay { 361aff268cdSMatt Ranostay int ret = -EINVAL; 362aff268cdSMatt Ranostay int idx; 363aff268cdSMatt Ranostay 364aff268cdSMatt Ranostay for (idx = 0; idx < ARRAY_SIZE(apds9960_int_time); idx++) { 365aff268cdSMatt Ranostay if (apds9960_int_time[idx][0] == val2) { 366aff268cdSMatt Ranostay mutex_lock(&data->lock); 367aff268cdSMatt Ranostay ret = regmap_write(data->regmap, APDS9960_REG_ATIME, 368aff268cdSMatt Ranostay apds9960_int_time[idx][1]); 369aff268cdSMatt Ranostay if (!ret) 370aff268cdSMatt Ranostay data->als_adc_int_us = val2; 371aff268cdSMatt Ranostay mutex_unlock(&data->lock); 372aff268cdSMatt Ranostay break; 373aff268cdSMatt Ranostay } 374aff268cdSMatt Ranostay } 375aff268cdSMatt Ranostay 376aff268cdSMatt Ranostay return ret; 377aff268cdSMatt Ranostay } 378aff268cdSMatt Ranostay 379aff268cdSMatt Ranostay static int apds9960_set_pxs_gain(struct apds9960_data *data, int val) 380aff268cdSMatt Ranostay { 381aff268cdSMatt Ranostay int ret = -EINVAL; 382aff268cdSMatt Ranostay int idx; 383aff268cdSMatt Ranostay 384aff268cdSMatt Ranostay for (idx = 0; idx < ARRAY_SIZE(apds9960_pxs_gain_map); idx++) { 385aff268cdSMatt Ranostay if (apds9960_pxs_gain_map[idx] == val) { 386aff268cdSMatt Ranostay /* pxs + gesture gains are mirrored */ 387aff268cdSMatt Ranostay mutex_lock(&data->lock); 388aff268cdSMatt Ranostay ret = regmap_update_bits(data->regmap, 389aff268cdSMatt Ranostay APDS9960_REG_CONTROL, 390aff268cdSMatt Ranostay APDS9960_REG_CONTROL_PGAIN_MASK, 391aff268cdSMatt Ranostay idx << APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT); 392aff268cdSMatt Ranostay if (ret) { 393aff268cdSMatt Ranostay mutex_unlock(&data->lock); 394aff268cdSMatt Ranostay break; 395aff268cdSMatt Ranostay } 396aff268cdSMatt Ranostay 397aff268cdSMatt Ranostay ret = regmap_update_bits(data->regmap, 398aff268cdSMatt Ranostay APDS9960_REG_CONFIG_2, 399aff268cdSMatt Ranostay APDS9960_REG_CONFIG_2_GGAIN_MASK, 400aff268cdSMatt Ranostay idx << APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT); 401aff268cdSMatt Ranostay if (!ret) 402aff268cdSMatt Ranostay data->pxs_gain = idx; 403aff268cdSMatt Ranostay mutex_unlock(&data->lock); 404aff268cdSMatt Ranostay break; 405aff268cdSMatt Ranostay } 406aff268cdSMatt Ranostay } 407aff268cdSMatt Ranostay 408aff268cdSMatt Ranostay return ret; 409aff268cdSMatt Ranostay } 410aff268cdSMatt Ranostay 411aff268cdSMatt Ranostay static int apds9960_set_als_gain(struct apds9960_data *data, int val) 412aff268cdSMatt Ranostay { 413aff268cdSMatt Ranostay int ret = -EINVAL; 414aff268cdSMatt Ranostay int idx; 415aff268cdSMatt Ranostay 416aff268cdSMatt Ranostay for (idx = 0; idx < ARRAY_SIZE(apds9960_als_gain_map); idx++) { 417aff268cdSMatt Ranostay if (apds9960_als_gain_map[idx] == val) { 418aff268cdSMatt Ranostay mutex_lock(&data->lock); 419aff268cdSMatt Ranostay ret = regmap_update_bits(data->regmap, 420aff268cdSMatt Ranostay APDS9960_REG_CONTROL, 421aff268cdSMatt Ranostay APDS9960_REG_CONTROL_AGAIN_MASK, idx); 422aff268cdSMatt Ranostay if (!ret) 423aff268cdSMatt Ranostay data->als_gain = idx; 424aff268cdSMatt Ranostay mutex_unlock(&data->lock); 425aff268cdSMatt Ranostay break; 426aff268cdSMatt Ranostay } 427aff268cdSMatt Ranostay } 428aff268cdSMatt Ranostay 429aff268cdSMatt Ranostay return ret; 430aff268cdSMatt Ranostay } 431aff268cdSMatt Ranostay 432aff268cdSMatt Ranostay #ifdef CONFIG_PM 433aff268cdSMatt Ranostay static int apds9960_set_power_state(struct apds9960_data *data, bool on) 434aff268cdSMatt Ranostay { 435aff268cdSMatt Ranostay struct device *dev = &data->client->dev; 436aff268cdSMatt Ranostay int ret = 0; 437aff268cdSMatt Ranostay 438aff268cdSMatt Ranostay mutex_lock(&data->lock); 439aff268cdSMatt Ranostay 440aff268cdSMatt Ranostay if (on) { 441aff268cdSMatt Ranostay int suspended; 442aff268cdSMatt Ranostay 443aff268cdSMatt Ranostay suspended = pm_runtime_suspended(dev); 444aff268cdSMatt Ranostay ret = pm_runtime_get_sync(dev); 445aff268cdSMatt Ranostay 446aff268cdSMatt Ranostay /* Allow one integration cycle before allowing a reading */ 447aff268cdSMatt Ranostay if (suspended) 448aff268cdSMatt Ranostay usleep_range(data->als_adc_int_us, 449aff268cdSMatt Ranostay APDS9960_MAX_INT_TIME_IN_US); 450aff268cdSMatt Ranostay } else { 4518386c275SMatt Ranostay pm_runtime_mark_last_busy(dev); 452aff268cdSMatt Ranostay ret = pm_runtime_put_autosuspend(dev); 453aff268cdSMatt Ranostay } 454aff268cdSMatt Ranostay 455aff268cdSMatt Ranostay mutex_unlock(&data->lock); 456aff268cdSMatt Ranostay 457aff268cdSMatt Ranostay return ret; 458aff268cdSMatt Ranostay } 459aff268cdSMatt Ranostay #else 460aff268cdSMatt Ranostay static int apds9960_set_power_state(struct apds9960_data *data, bool on) 461aff268cdSMatt Ranostay { 462aff268cdSMatt Ranostay return 0; 463aff268cdSMatt Ranostay } 464aff268cdSMatt Ranostay #endif 465aff268cdSMatt Ranostay 466aff268cdSMatt Ranostay static int apds9960_read_raw(struct iio_dev *indio_dev, 467aff268cdSMatt Ranostay struct iio_chan_spec const *chan, 468aff268cdSMatt Ranostay int *val, int *val2, long mask) 469aff268cdSMatt Ranostay { 470aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 471801ab335SJonathan Cameron __le16 buf; 472aff268cdSMatt Ranostay int ret = -EINVAL; 473aff268cdSMatt Ranostay 474aff268cdSMatt Ranostay if (data->gesture_mode_running) 475aff268cdSMatt Ranostay return -EBUSY; 476aff268cdSMatt Ranostay 477aff268cdSMatt Ranostay switch (mask) { 478aff268cdSMatt Ranostay case IIO_CHAN_INFO_RAW: 479aff268cdSMatt Ranostay apds9960_set_power_state(data, true); 480aff268cdSMatt Ranostay switch (chan->type) { 481aff268cdSMatt Ranostay case IIO_PROXIMITY: 482aff268cdSMatt Ranostay ret = regmap_read(data->regmap, chan->address, val); 483aff268cdSMatt Ranostay if (!ret) 484aff268cdSMatt Ranostay ret = IIO_VAL_INT; 485aff268cdSMatt Ranostay break; 486aff268cdSMatt Ranostay case IIO_INTENSITY: 487aff268cdSMatt Ranostay ret = regmap_bulk_read(data->regmap, chan->address, 488aff268cdSMatt Ranostay &buf, 2); 489f2112259SDan Carpenter if (!ret) { 490aff268cdSMatt Ranostay ret = IIO_VAL_INT; 491aff268cdSMatt Ranostay *val = le16_to_cpu(buf); 492f2112259SDan Carpenter } 493aff268cdSMatt Ranostay break; 494aff268cdSMatt Ranostay default: 495aff268cdSMatt Ranostay ret = -EINVAL; 496aff268cdSMatt Ranostay } 497aff268cdSMatt Ranostay apds9960_set_power_state(data, false); 498aff268cdSMatt Ranostay break; 499aff268cdSMatt Ranostay case IIO_CHAN_INFO_INT_TIME: 500aff268cdSMatt Ranostay /* RGB + ALS sensors only have integration time */ 501aff268cdSMatt Ranostay mutex_lock(&data->lock); 502aff268cdSMatt Ranostay switch (chan->type) { 503aff268cdSMatt Ranostay case IIO_INTENSITY: 504aff268cdSMatt Ranostay *val = 0; 505aff268cdSMatt Ranostay *val2 = data->als_adc_int_us; 506aff268cdSMatt Ranostay ret = IIO_VAL_INT_PLUS_MICRO; 507aff268cdSMatt Ranostay break; 508aff268cdSMatt Ranostay default: 509aff268cdSMatt Ranostay ret = -EINVAL; 510aff268cdSMatt Ranostay } 511aff268cdSMatt Ranostay mutex_unlock(&data->lock); 512aff268cdSMatt Ranostay break; 513aff268cdSMatt Ranostay case IIO_CHAN_INFO_SCALE: 514aff268cdSMatt Ranostay mutex_lock(&data->lock); 515aff268cdSMatt Ranostay switch (chan->type) { 516aff268cdSMatt Ranostay case IIO_PROXIMITY: 517aff268cdSMatt Ranostay *val = apds9960_pxs_gain_map[data->pxs_gain]; 518aff268cdSMatt Ranostay ret = IIO_VAL_INT; 519aff268cdSMatt Ranostay break; 520aff268cdSMatt Ranostay case IIO_INTENSITY: 521aff268cdSMatt Ranostay *val = apds9960_als_gain_map[data->als_gain]; 522aff268cdSMatt Ranostay ret = IIO_VAL_INT; 523aff268cdSMatt Ranostay break; 524aff268cdSMatt Ranostay default: 525aff268cdSMatt Ranostay ret = -EINVAL; 526aff268cdSMatt Ranostay } 527aff268cdSMatt Ranostay mutex_unlock(&data->lock); 528aff268cdSMatt Ranostay break; 529aff268cdSMatt Ranostay } 530aff268cdSMatt Ranostay 531aff268cdSMatt Ranostay return ret; 532aff268cdSMatt Ranostay }; 533aff268cdSMatt Ranostay 534aff268cdSMatt Ranostay static int apds9960_write_raw(struct iio_dev *indio_dev, 535aff268cdSMatt Ranostay struct iio_chan_spec const *chan, 536aff268cdSMatt Ranostay int val, int val2, long mask) 537aff268cdSMatt Ranostay { 538aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 539aff268cdSMatt Ranostay 540aff268cdSMatt Ranostay switch (mask) { 541aff268cdSMatt Ranostay case IIO_CHAN_INFO_INT_TIME: 542aff268cdSMatt Ranostay /* RGB + ALS sensors only have int time */ 543aff268cdSMatt Ranostay switch (chan->type) { 544aff268cdSMatt Ranostay case IIO_INTENSITY: 545aff268cdSMatt Ranostay if (val != 0) 546aff268cdSMatt Ranostay return -EINVAL; 547aff268cdSMatt Ranostay return apds9960_set_it_time(data, val2); 548aff268cdSMatt Ranostay default: 549aff268cdSMatt Ranostay return -EINVAL; 550aff268cdSMatt Ranostay } 551aff268cdSMatt Ranostay case IIO_CHAN_INFO_SCALE: 552aff268cdSMatt Ranostay if (val2 != 0) 553aff268cdSMatt Ranostay return -EINVAL; 554aff268cdSMatt Ranostay switch (chan->type) { 555aff268cdSMatt Ranostay case IIO_PROXIMITY: 556aff268cdSMatt Ranostay return apds9960_set_pxs_gain(data, val); 557aff268cdSMatt Ranostay case IIO_INTENSITY: 558aff268cdSMatt Ranostay return apds9960_set_als_gain(data, val); 559aff268cdSMatt Ranostay default: 560aff268cdSMatt Ranostay return -EINVAL; 561aff268cdSMatt Ranostay } 562aff268cdSMatt Ranostay default: 563aff268cdSMatt Ranostay return -EINVAL; 564*691f4539STom Rix } 565aff268cdSMatt Ranostay 566aff268cdSMatt Ranostay return 0; 567aff268cdSMatt Ranostay } 568aff268cdSMatt Ranostay 569aff268cdSMatt Ranostay static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan, 570aff268cdSMatt Ranostay enum iio_event_direction dir, 571aff268cdSMatt Ranostay u8 *reg) 572aff268cdSMatt Ranostay { 573aff268cdSMatt Ranostay switch (dir) { 574aff268cdSMatt Ranostay case IIO_EV_DIR_RISING: 575aff268cdSMatt Ranostay switch (chan->type) { 576aff268cdSMatt Ranostay case IIO_PROXIMITY: 577aff268cdSMatt Ranostay *reg = APDS9960_REG_PIHT; 578aff268cdSMatt Ranostay break; 579aff268cdSMatt Ranostay case IIO_INTENSITY: 580aff268cdSMatt Ranostay *reg = APDS9960_REG_AIHTL; 581aff268cdSMatt Ranostay break; 582aff268cdSMatt Ranostay default: 583aff268cdSMatt Ranostay return -EINVAL; 584aff268cdSMatt Ranostay } 585aff268cdSMatt Ranostay break; 586aff268cdSMatt Ranostay case IIO_EV_DIR_FALLING: 587aff268cdSMatt Ranostay switch (chan->type) { 588aff268cdSMatt Ranostay case IIO_PROXIMITY: 589aff268cdSMatt Ranostay *reg = APDS9960_REG_PILT; 590aff268cdSMatt Ranostay break; 591aff268cdSMatt Ranostay case IIO_INTENSITY: 592aff268cdSMatt Ranostay *reg = APDS9960_REG_AILTL; 593aff268cdSMatt Ranostay break; 594aff268cdSMatt Ranostay default: 595aff268cdSMatt Ranostay return -EINVAL; 596aff268cdSMatt Ranostay } 597aff268cdSMatt Ranostay break; 598aff268cdSMatt Ranostay default: 599aff268cdSMatt Ranostay return -EINVAL; 600aff268cdSMatt Ranostay } 601aff268cdSMatt Ranostay 602aff268cdSMatt Ranostay return 0; 603aff268cdSMatt Ranostay } 604aff268cdSMatt Ranostay 605aff268cdSMatt Ranostay static int apds9960_read_event(struct iio_dev *indio_dev, 606aff268cdSMatt Ranostay const struct iio_chan_spec *chan, 607aff268cdSMatt Ranostay enum iio_event_type type, 608aff268cdSMatt Ranostay enum iio_event_direction dir, 609aff268cdSMatt Ranostay enum iio_event_info info, 610aff268cdSMatt Ranostay int *val, int *val2) 611aff268cdSMatt Ranostay { 612aff268cdSMatt Ranostay u8 reg; 613801ab335SJonathan Cameron __le16 buf; 614aff268cdSMatt Ranostay int ret = 0; 615aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 616aff268cdSMatt Ranostay 617aff268cdSMatt Ranostay if (info != IIO_EV_INFO_VALUE) 618aff268cdSMatt Ranostay return -EINVAL; 619aff268cdSMatt Ranostay 620aff268cdSMatt Ranostay ret = apds9960_get_thres_reg(chan, dir, ®); 621aff268cdSMatt Ranostay if (ret < 0) 622aff268cdSMatt Ranostay return ret; 623aff268cdSMatt Ranostay 624aff268cdSMatt Ranostay if (chan->type == IIO_PROXIMITY) { 625aff268cdSMatt Ranostay ret = regmap_read(data->regmap, reg, val); 626aff268cdSMatt Ranostay if (ret < 0) 627aff268cdSMatt Ranostay return ret; 628aff268cdSMatt Ranostay } else if (chan->type == IIO_INTENSITY) { 629aff268cdSMatt Ranostay ret = regmap_bulk_read(data->regmap, reg, &buf, 2); 630aff268cdSMatt Ranostay if (ret < 0) 631aff268cdSMatt Ranostay return ret; 632aff268cdSMatt Ranostay *val = le16_to_cpu(buf); 633aff268cdSMatt Ranostay } else 634aff268cdSMatt Ranostay return -EINVAL; 635aff268cdSMatt Ranostay 636aff268cdSMatt Ranostay *val2 = 0; 637aff268cdSMatt Ranostay 638aff268cdSMatt Ranostay return IIO_VAL_INT; 639aff268cdSMatt Ranostay } 640aff268cdSMatt Ranostay 641aff268cdSMatt Ranostay static int apds9960_write_event(struct iio_dev *indio_dev, 642aff268cdSMatt Ranostay const struct iio_chan_spec *chan, 643aff268cdSMatt Ranostay enum iio_event_type type, 644aff268cdSMatt Ranostay enum iio_event_direction dir, 645aff268cdSMatt Ranostay enum iio_event_info info, 646aff268cdSMatt Ranostay int val, int val2) 647aff268cdSMatt Ranostay { 648aff268cdSMatt Ranostay u8 reg; 649801ab335SJonathan Cameron __le16 buf; 650aff268cdSMatt Ranostay int ret = 0; 651aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 652aff268cdSMatt Ranostay 653aff268cdSMatt Ranostay if (info != IIO_EV_INFO_VALUE) 654aff268cdSMatt Ranostay return -EINVAL; 655aff268cdSMatt Ranostay 656aff268cdSMatt Ranostay ret = apds9960_get_thres_reg(chan, dir, ®); 657aff268cdSMatt Ranostay if (ret < 0) 658aff268cdSMatt Ranostay return ret; 659aff268cdSMatt Ranostay 660aff268cdSMatt Ranostay if (chan->type == IIO_PROXIMITY) { 661aff268cdSMatt Ranostay if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL) 662aff268cdSMatt Ranostay return -EINVAL; 663aff268cdSMatt Ranostay ret = regmap_write(data->regmap, reg, val); 664aff268cdSMatt Ranostay if (ret < 0) 665aff268cdSMatt Ranostay return ret; 666aff268cdSMatt Ranostay } else if (chan->type == IIO_INTENSITY) { 667aff268cdSMatt Ranostay if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL) 668aff268cdSMatt Ranostay return -EINVAL; 669aff268cdSMatt Ranostay buf = cpu_to_le16(val); 670aff268cdSMatt Ranostay ret = regmap_bulk_write(data->regmap, reg, &buf, 2); 671aff268cdSMatt Ranostay if (ret < 0) 672aff268cdSMatt Ranostay return ret; 673aff268cdSMatt Ranostay } else 674aff268cdSMatt Ranostay return -EINVAL; 675aff268cdSMatt Ranostay 676aff268cdSMatt Ranostay return 0; 677aff268cdSMatt Ranostay } 678aff268cdSMatt Ranostay 679aff268cdSMatt Ranostay static int apds9960_read_event_config(struct iio_dev *indio_dev, 680aff268cdSMatt Ranostay const struct iio_chan_spec *chan, 681aff268cdSMatt Ranostay enum iio_event_type type, 682aff268cdSMatt Ranostay enum iio_event_direction dir) 683aff268cdSMatt Ranostay { 684aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 685aff268cdSMatt Ranostay 686aff268cdSMatt Ranostay switch (chan->type) { 687aff268cdSMatt Ranostay case IIO_PROXIMITY: 688aff268cdSMatt Ranostay return data->pxs_int; 689aff268cdSMatt Ranostay case IIO_INTENSITY: 690aff268cdSMatt Ranostay return data->als_int; 691aff268cdSMatt Ranostay default: 692aff268cdSMatt Ranostay return -EINVAL; 693aff268cdSMatt Ranostay } 694aff268cdSMatt Ranostay 695aff268cdSMatt Ranostay return 0; 696aff268cdSMatt Ranostay } 697aff268cdSMatt Ranostay 698aff268cdSMatt Ranostay static int apds9960_write_event_config(struct iio_dev *indio_dev, 699aff268cdSMatt Ranostay const struct iio_chan_spec *chan, 700aff268cdSMatt Ranostay enum iio_event_type type, 701aff268cdSMatt Ranostay enum iio_event_direction dir, 702aff268cdSMatt Ranostay int state) 703aff268cdSMatt Ranostay { 704aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 705aff268cdSMatt Ranostay int ret; 706aff268cdSMatt Ranostay 707aff268cdSMatt Ranostay state = !!state; 708aff268cdSMatt Ranostay 709aff268cdSMatt Ranostay switch (chan->type) { 710aff268cdSMatt Ranostay case IIO_PROXIMITY: 711aff268cdSMatt Ranostay if (data->pxs_int == state) 712aff268cdSMatt Ranostay return -EINVAL; 713aff268cdSMatt Ranostay 714aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_int_pxs, state); 715aff268cdSMatt Ranostay if (ret) 716aff268cdSMatt Ranostay return ret; 717aff268cdSMatt Ranostay data->pxs_int = state; 718aff268cdSMatt Ranostay apds9960_set_power_state(data, state); 719aff268cdSMatt Ranostay break; 720aff268cdSMatt Ranostay case IIO_INTENSITY: 721aff268cdSMatt Ranostay if (data->als_int == state) 722aff268cdSMatt Ranostay return -EINVAL; 723aff268cdSMatt Ranostay 724aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_int_als, state); 725aff268cdSMatt Ranostay if (ret) 726aff268cdSMatt Ranostay return ret; 727aff268cdSMatt Ranostay data->als_int = state; 728aff268cdSMatt Ranostay apds9960_set_power_state(data, state); 729aff268cdSMatt Ranostay break; 730aff268cdSMatt Ranostay default: 731aff268cdSMatt Ranostay return -EINVAL; 732aff268cdSMatt Ranostay } 733aff268cdSMatt Ranostay 734aff268cdSMatt Ranostay return 0; 735aff268cdSMatt Ranostay } 736aff268cdSMatt Ranostay 737aff268cdSMatt Ranostay static const struct iio_info apds9960_info = { 738aff268cdSMatt Ranostay .attrs = &apds9960_attribute_group, 739aff268cdSMatt Ranostay .read_raw = apds9960_read_raw, 740aff268cdSMatt Ranostay .write_raw = apds9960_write_raw, 741aff268cdSMatt Ranostay .read_event_value = apds9960_read_event, 742aff268cdSMatt Ranostay .write_event_value = apds9960_write_event, 743aff268cdSMatt Ranostay .read_event_config = apds9960_read_event_config, 744aff268cdSMatt Ranostay .write_event_config = apds9960_write_event_config, 745aff268cdSMatt Ranostay 746aff268cdSMatt Ranostay }; 747aff268cdSMatt Ranostay 748aff268cdSMatt Ranostay static inline int apds9660_fifo_is_empty(struct apds9960_data *data) 749aff268cdSMatt Ranostay { 750aff268cdSMatt Ranostay int cnt; 751aff268cdSMatt Ranostay int ret; 752aff268cdSMatt Ranostay 753aff268cdSMatt Ranostay ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt); 754aff268cdSMatt Ranostay if (ret) 755aff268cdSMatt Ranostay return ret; 756aff268cdSMatt Ranostay 757aff268cdSMatt Ranostay return cnt; 758aff268cdSMatt Ranostay } 759aff268cdSMatt Ranostay 760aff268cdSMatt Ranostay static void apds9960_read_gesture_fifo(struct apds9960_data *data) 761aff268cdSMatt Ranostay { 762aff268cdSMatt Ranostay int ret, cnt = 0; 763aff268cdSMatt Ranostay 764aff268cdSMatt Ranostay mutex_lock(&data->lock); 765aff268cdSMatt Ranostay data->gesture_mode_running = 1; 766aff268cdSMatt Ranostay 767655048a0SMatt Ranostay while (cnt || (cnt = apds9660_fifo_is_empty(data) > 0)) { 768aff268cdSMatt Ranostay ret = regmap_bulk_read(data->regmap, APDS9960_REG_GFIFO_BASE, 769aff268cdSMatt Ranostay &data->buffer, 4); 770aff268cdSMatt Ranostay 771aff268cdSMatt Ranostay if (ret) 772aff268cdSMatt Ranostay goto err_read; 773aff268cdSMatt Ranostay 774aff268cdSMatt Ranostay iio_push_to_buffers(data->indio_dev, data->buffer); 775655048a0SMatt Ranostay cnt--; 776aff268cdSMatt Ranostay } 777aff268cdSMatt Ranostay 778aff268cdSMatt Ranostay err_read: 779aff268cdSMatt Ranostay data->gesture_mode_running = 0; 780aff268cdSMatt Ranostay mutex_unlock(&data->lock); 781aff268cdSMatt Ranostay } 782aff268cdSMatt Ranostay 783aff268cdSMatt Ranostay static irqreturn_t apds9960_interrupt_handler(int irq, void *private) 784aff268cdSMatt Ranostay { 785aff268cdSMatt Ranostay struct iio_dev *indio_dev = private; 786aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 787aff268cdSMatt Ranostay int ret, status; 788aff268cdSMatt Ranostay 789aff268cdSMatt Ranostay ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status); 790aff268cdSMatt Ranostay if (ret < 0) { 791aff268cdSMatt Ranostay dev_err(&data->client->dev, "irq status reg read failed\n"); 792aff268cdSMatt Ranostay return IRQ_HANDLED; 793aff268cdSMatt Ranostay } 794aff268cdSMatt Ranostay 795aff268cdSMatt Ranostay if ((status & APDS9960_REG_STATUS_ALS_INT) && data->als_int) { 796aff268cdSMatt Ranostay iio_push_event(indio_dev, 797aff268cdSMatt Ranostay IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0, 798aff268cdSMatt Ranostay IIO_EV_TYPE_THRESH, 799aff268cdSMatt Ranostay IIO_EV_DIR_EITHER), 800bc2b7dabSGregor Boirie iio_get_time_ns(indio_dev)); 801aff268cdSMatt Ranostay regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1); 802aff268cdSMatt Ranostay } 803aff268cdSMatt Ranostay 804aff268cdSMatt Ranostay if ((status & APDS9960_REG_STATUS_PS_INT) && data->pxs_int) { 805aff268cdSMatt Ranostay iio_push_event(indio_dev, 806aff268cdSMatt Ranostay IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0, 807aff268cdSMatt Ranostay IIO_EV_TYPE_THRESH, 808aff268cdSMatt Ranostay IIO_EV_DIR_EITHER), 809bc2b7dabSGregor Boirie iio_get_time_ns(indio_dev)); 810aff268cdSMatt Ranostay regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1); 811aff268cdSMatt Ranostay } 812aff268cdSMatt Ranostay 813aff268cdSMatt Ranostay if (status & APDS9960_REG_STATUS_GINT) 814aff268cdSMatt Ranostay apds9960_read_gesture_fifo(data); 815aff268cdSMatt Ranostay 816aff268cdSMatt Ranostay return IRQ_HANDLED; 817aff268cdSMatt Ranostay } 818aff268cdSMatt Ranostay 819aff268cdSMatt Ranostay static int apds9960_set_powermode(struct apds9960_data *data, bool state) 820aff268cdSMatt Ranostay { 821aff268cdSMatt Ranostay return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state); 822aff268cdSMatt Ranostay } 823aff268cdSMatt Ranostay 824aff268cdSMatt Ranostay static int apds9960_buffer_postenable(struct iio_dev *indio_dev) 825aff268cdSMatt Ranostay { 826aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 827aff268cdSMatt Ranostay int ret; 828aff268cdSMatt Ranostay 829aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_int_ges, 1); 830aff268cdSMatt Ranostay if (ret) 831aff268cdSMatt Ranostay return ret; 832aff268cdSMatt Ranostay 833aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_enable_ges, 1); 834aff268cdSMatt Ranostay if (ret) 835aff268cdSMatt Ranostay return ret; 836aff268cdSMatt Ranostay 837aff268cdSMatt Ranostay pm_runtime_get_sync(&data->client->dev); 838aff268cdSMatt Ranostay 839aff268cdSMatt Ranostay return 0; 840aff268cdSMatt Ranostay } 841aff268cdSMatt Ranostay 842aff268cdSMatt Ranostay static int apds9960_buffer_predisable(struct iio_dev *indio_dev) 843aff268cdSMatt Ranostay { 844aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 845aff268cdSMatt Ranostay int ret; 846aff268cdSMatt Ranostay 847aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_enable_ges, 0); 848aff268cdSMatt Ranostay if (ret) 849aff268cdSMatt Ranostay return ret; 850aff268cdSMatt Ranostay 851aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_int_ges, 0); 852aff268cdSMatt Ranostay if (ret) 853aff268cdSMatt Ranostay return ret; 854aff268cdSMatt Ranostay 855aff268cdSMatt Ranostay pm_runtime_put_autosuspend(&data->client->dev); 856aff268cdSMatt Ranostay 857aff268cdSMatt Ranostay return 0; 858aff268cdSMatt Ranostay } 859aff268cdSMatt Ranostay 860aff268cdSMatt Ranostay static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = { 861aff268cdSMatt Ranostay .postenable = apds9960_buffer_postenable, 862aff268cdSMatt Ranostay .predisable = apds9960_buffer_predisable, 863aff268cdSMatt Ranostay }; 864aff268cdSMatt Ranostay 865aff268cdSMatt Ranostay static int apds9960_regfield_init(struct apds9960_data *data) 866aff268cdSMatt Ranostay { 867aff268cdSMatt Ranostay struct device *dev = &data->client->dev; 868aff268cdSMatt Ranostay struct regmap *regmap = data->regmap; 869aff268cdSMatt Ranostay 870aff268cdSMatt Ranostay data->reg_int_als = devm_regmap_field_alloc(dev, regmap, 871aff268cdSMatt Ranostay apds9960_reg_field_int_als); 872aff268cdSMatt Ranostay if (IS_ERR(data->reg_int_als)) { 873aff268cdSMatt Ranostay dev_err(dev, "INT ALS reg field init failed\n"); 874aff268cdSMatt Ranostay return PTR_ERR(data->reg_int_als); 875aff268cdSMatt Ranostay } 876aff268cdSMatt Ranostay 877aff268cdSMatt Ranostay data->reg_int_ges = devm_regmap_field_alloc(dev, regmap, 878aff268cdSMatt Ranostay apds9960_reg_field_int_ges); 879aff268cdSMatt Ranostay if (IS_ERR(data->reg_int_ges)) { 880aff268cdSMatt Ranostay dev_err(dev, "INT gesture reg field init failed\n"); 881aff268cdSMatt Ranostay return PTR_ERR(data->reg_int_ges); 882aff268cdSMatt Ranostay } 883aff268cdSMatt Ranostay 884aff268cdSMatt Ranostay data->reg_int_pxs = devm_regmap_field_alloc(dev, regmap, 885aff268cdSMatt Ranostay apds9960_reg_field_int_pxs); 886aff268cdSMatt Ranostay if (IS_ERR(data->reg_int_pxs)) { 887aff268cdSMatt Ranostay dev_err(dev, "INT pxs reg field init failed\n"); 888aff268cdSMatt Ranostay return PTR_ERR(data->reg_int_pxs); 889aff268cdSMatt Ranostay } 890aff268cdSMatt Ranostay 891aff268cdSMatt Ranostay data->reg_enable_als = devm_regmap_field_alloc(dev, regmap, 892aff268cdSMatt Ranostay apds9960_reg_field_enable_als); 893aff268cdSMatt Ranostay if (IS_ERR(data->reg_enable_als)) { 894aff268cdSMatt Ranostay dev_err(dev, "Enable ALS reg field init failed\n"); 895aff268cdSMatt Ranostay return PTR_ERR(data->reg_enable_als); 896aff268cdSMatt Ranostay } 897aff268cdSMatt Ranostay 898aff268cdSMatt Ranostay data->reg_enable_ges = devm_regmap_field_alloc(dev, regmap, 899aff268cdSMatt Ranostay apds9960_reg_field_enable_ges); 900aff268cdSMatt Ranostay if (IS_ERR(data->reg_enable_ges)) { 901aff268cdSMatt Ranostay dev_err(dev, "Enable gesture reg field init failed\n"); 902aff268cdSMatt Ranostay return PTR_ERR(data->reg_enable_ges); 903aff268cdSMatt Ranostay } 904aff268cdSMatt Ranostay 905aff268cdSMatt Ranostay data->reg_enable_pxs = devm_regmap_field_alloc(dev, regmap, 906aff268cdSMatt Ranostay apds9960_reg_field_enable_pxs); 907aff268cdSMatt Ranostay if (IS_ERR(data->reg_enable_pxs)) { 908aff268cdSMatt Ranostay dev_err(dev, "Enable PXS reg field init failed\n"); 909aff268cdSMatt Ranostay return PTR_ERR(data->reg_enable_pxs); 910aff268cdSMatt Ranostay } 911aff268cdSMatt Ranostay 912aff268cdSMatt Ranostay return 0; 913aff268cdSMatt Ranostay } 914aff268cdSMatt Ranostay 915aff268cdSMatt Ranostay static int apds9960_chip_init(struct apds9960_data *data) 916aff268cdSMatt Ranostay { 917aff268cdSMatt Ranostay int ret; 918aff268cdSMatt Ranostay 919aff268cdSMatt Ranostay /* Default IT for ALS of 28 ms */ 920aff268cdSMatt Ranostay ret = apds9960_set_it_time(data, 28000); 921aff268cdSMatt Ranostay if (ret) 922aff268cdSMatt Ranostay return ret; 923aff268cdSMatt Ranostay 924aff268cdSMatt Ranostay /* Ensure gesture interrupt is OFF */ 925aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_int_ges, 0); 926aff268cdSMatt Ranostay if (ret) 927aff268cdSMatt Ranostay return ret; 928aff268cdSMatt Ranostay 929aff268cdSMatt Ranostay /* Disable gesture sensor, since polling is useless from user-space */ 930aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_enable_ges, 0); 931aff268cdSMatt Ranostay if (ret) 932aff268cdSMatt Ranostay return ret; 933aff268cdSMatt Ranostay 934aff268cdSMatt Ranostay /* Ensure proximity interrupt is OFF */ 935aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_int_pxs, 0); 936aff268cdSMatt Ranostay if (ret) 937aff268cdSMatt Ranostay return ret; 938aff268cdSMatt Ranostay 939aff268cdSMatt Ranostay /* Enable proximity sensor for polling */ 940aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_enable_pxs, 1); 941aff268cdSMatt Ranostay if (ret) 942aff268cdSMatt Ranostay return ret; 943aff268cdSMatt Ranostay 944aff268cdSMatt Ranostay /* Ensure ALS interrupt is OFF */ 945aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_int_als, 0); 946aff268cdSMatt Ranostay if (ret) 947aff268cdSMatt Ranostay return ret; 948aff268cdSMatt Ranostay 949aff268cdSMatt Ranostay /* Enable ALS sensor for polling */ 950aff268cdSMatt Ranostay ret = regmap_field_write(data->reg_enable_als, 1); 951aff268cdSMatt Ranostay if (ret) 952aff268cdSMatt Ranostay return ret; 953aff268cdSMatt Ranostay /* 954aff268cdSMatt Ranostay * When enabled trigger an interrupt after 3 readings 955aff268cdSMatt Ranostay * outside threshold for ALS + PXS 956aff268cdSMatt Ranostay */ 957aff268cdSMatt Ranostay ret = regmap_write(data->regmap, APDS9960_REG_PERS, 958aff268cdSMatt Ranostay APDS9960_DEFAULT_PERS); 959aff268cdSMatt Ranostay if (ret) 960aff268cdSMatt Ranostay return ret; 961aff268cdSMatt Ranostay 962aff268cdSMatt Ranostay /* 963aff268cdSMatt Ranostay * Wait for 4 event outside gesture threshold to prevent interrupt 964aff268cdSMatt Ranostay * flooding. 965aff268cdSMatt Ranostay */ 966aff268cdSMatt Ranostay ret = regmap_update_bits(data->regmap, APDS9960_REG_GCONF_1, 967aff268cdSMatt Ranostay APDS9960_REG_GCONF_1_GFIFO_THRES_MASK, 968aff268cdSMatt Ranostay BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT); 969aff268cdSMatt Ranostay if (ret) 970aff268cdSMatt Ranostay return ret; 971aff268cdSMatt Ranostay 972aff268cdSMatt Ranostay /* Default ENTER and EXIT thresholds for the GESTURE engine. */ 973aff268cdSMatt Ranostay ret = regmap_write(data->regmap, APDS9960_REG_GPENTH, 974aff268cdSMatt Ranostay APDS9960_DEFAULT_GPENTH); 975aff268cdSMatt Ranostay if (ret) 976aff268cdSMatt Ranostay return ret; 977aff268cdSMatt Ranostay 978aff268cdSMatt Ranostay ret = regmap_write(data->regmap, APDS9960_REG_GEXTH, 979aff268cdSMatt Ranostay APDS9960_DEFAULT_GEXTH); 980aff268cdSMatt Ranostay if (ret) 981aff268cdSMatt Ranostay return ret; 982aff268cdSMatt Ranostay 983ebe5c543Skbuild test robot return apds9960_set_powermode(data, 1); 984aff268cdSMatt Ranostay } 985aff268cdSMatt Ranostay 986aff268cdSMatt Ranostay static int apds9960_probe(struct i2c_client *client, 987aff268cdSMatt Ranostay const struct i2c_device_id *id) 988aff268cdSMatt Ranostay { 989aff268cdSMatt Ranostay struct apds9960_data *data; 990aff268cdSMatt Ranostay struct iio_buffer *buffer; 991aff268cdSMatt Ranostay struct iio_dev *indio_dev; 992aff268cdSMatt Ranostay int ret; 993aff268cdSMatt Ranostay 994aff268cdSMatt Ranostay indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 995aff268cdSMatt Ranostay if (!indio_dev) 996aff268cdSMatt Ranostay return -ENOMEM; 997aff268cdSMatt Ranostay 998aff268cdSMatt Ranostay buffer = devm_iio_kfifo_allocate(&client->dev); 999aff268cdSMatt Ranostay if (!buffer) 1000aff268cdSMatt Ranostay return -ENOMEM; 1001aff268cdSMatt Ranostay 1002aff268cdSMatt Ranostay iio_device_attach_buffer(indio_dev, buffer); 1003aff268cdSMatt Ranostay 1004aff268cdSMatt Ranostay indio_dev->info = &apds9960_info; 1005aff268cdSMatt Ranostay indio_dev->name = APDS9960_DRV_NAME; 1006aff268cdSMatt Ranostay indio_dev->channels = apds9960_channels; 1007aff268cdSMatt Ranostay indio_dev->num_channels = ARRAY_SIZE(apds9960_channels); 1008aff268cdSMatt Ranostay indio_dev->available_scan_masks = apds9960_scan_masks; 1009aff268cdSMatt Ranostay indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE); 1010aff268cdSMatt Ranostay indio_dev->setup_ops = &apds9960_buffer_setup_ops; 1011aff268cdSMatt Ranostay 1012aff268cdSMatt Ranostay data = iio_priv(indio_dev); 1013aff268cdSMatt Ranostay i2c_set_clientdata(client, indio_dev); 1014aff268cdSMatt Ranostay 1015aff268cdSMatt Ranostay data->regmap = devm_regmap_init_i2c(client, &apds9960_regmap_config); 1016aff268cdSMatt Ranostay if (IS_ERR(data->regmap)) { 1017aff268cdSMatt Ranostay dev_err(&client->dev, "regmap initialization failed.\n"); 1018aff268cdSMatt Ranostay return PTR_ERR(data->regmap); 1019aff268cdSMatt Ranostay } 1020aff268cdSMatt Ranostay 1021aff268cdSMatt Ranostay data->client = client; 1022aff268cdSMatt Ranostay data->indio_dev = indio_dev; 1023aff268cdSMatt Ranostay mutex_init(&data->lock); 1024aff268cdSMatt Ranostay 1025aff268cdSMatt Ranostay ret = pm_runtime_set_active(&client->dev); 1026aff268cdSMatt Ranostay if (ret) 1027aff268cdSMatt Ranostay goto error_power_down; 1028aff268cdSMatt Ranostay 1029aff268cdSMatt Ranostay pm_runtime_enable(&client->dev); 1030aff268cdSMatt Ranostay pm_runtime_set_autosuspend_delay(&client->dev, 5000); 1031aff268cdSMatt Ranostay pm_runtime_use_autosuspend(&client->dev); 1032aff268cdSMatt Ranostay 1033aff268cdSMatt Ranostay apds9960_set_power_state(data, true); 1034aff268cdSMatt Ranostay 1035aff268cdSMatt Ranostay ret = apds9960_regfield_init(data); 1036aff268cdSMatt Ranostay if (ret) 1037aff268cdSMatt Ranostay goto error_power_down; 1038aff268cdSMatt Ranostay 1039aff268cdSMatt Ranostay ret = apds9960_chip_init(data); 1040aff268cdSMatt Ranostay if (ret) 1041aff268cdSMatt Ranostay goto error_power_down; 1042aff268cdSMatt Ranostay 1043aff268cdSMatt Ranostay if (client->irq <= 0) { 1044aff268cdSMatt Ranostay dev_err(&client->dev, "no valid irq defined\n"); 1045aff268cdSMatt Ranostay ret = -EINVAL; 1046aff268cdSMatt Ranostay goto error_power_down; 1047aff268cdSMatt Ranostay } 1048aff268cdSMatt Ranostay ret = devm_request_threaded_irq(&client->dev, client->irq, 1049aff268cdSMatt Ranostay NULL, apds9960_interrupt_handler, 1050aff268cdSMatt Ranostay IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1051aff268cdSMatt Ranostay "apds9960_event", 1052aff268cdSMatt Ranostay indio_dev); 1053aff268cdSMatt Ranostay if (ret) { 1054aff268cdSMatt Ranostay dev_err(&client->dev, "request irq (%d) failed\n", client->irq); 1055aff268cdSMatt Ranostay goto error_power_down; 1056aff268cdSMatt Ranostay } 1057aff268cdSMatt Ranostay 1058aff268cdSMatt Ranostay ret = iio_device_register(indio_dev); 1059aff268cdSMatt Ranostay if (ret) 1060aff268cdSMatt Ranostay goto error_power_down; 1061aff268cdSMatt Ranostay 1062aff268cdSMatt Ranostay apds9960_set_power_state(data, false); 1063aff268cdSMatt Ranostay 1064aff268cdSMatt Ranostay return 0; 1065aff268cdSMatt Ranostay 1066aff268cdSMatt Ranostay error_power_down: 1067aff268cdSMatt Ranostay apds9960_set_power_state(data, false); 1068aff268cdSMatt Ranostay 1069aff268cdSMatt Ranostay return ret; 1070aff268cdSMatt Ranostay } 1071aff268cdSMatt Ranostay 1072aff268cdSMatt Ranostay static int apds9960_remove(struct i2c_client *client) 1073aff268cdSMatt Ranostay { 1074aff268cdSMatt Ranostay struct iio_dev *indio_dev = i2c_get_clientdata(client); 1075aff268cdSMatt Ranostay struct apds9960_data *data = iio_priv(indio_dev); 1076aff268cdSMatt Ranostay 1077aff268cdSMatt Ranostay iio_device_unregister(indio_dev); 1078aff268cdSMatt Ranostay pm_runtime_disable(&client->dev); 1079aff268cdSMatt Ranostay pm_runtime_set_suspended(&client->dev); 1080aff268cdSMatt Ranostay apds9960_set_powermode(data, 0); 1081aff268cdSMatt Ranostay 1082aff268cdSMatt Ranostay return 0; 1083aff268cdSMatt Ranostay } 1084aff268cdSMatt Ranostay 1085aff268cdSMatt Ranostay #ifdef CONFIG_PM 1086aff268cdSMatt Ranostay static int apds9960_runtime_suspend(struct device *dev) 1087aff268cdSMatt Ranostay { 1088aff268cdSMatt Ranostay struct apds9960_data *data = 1089aff268cdSMatt Ranostay iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 1090aff268cdSMatt Ranostay 1091aff268cdSMatt Ranostay return apds9960_set_powermode(data, 0); 1092aff268cdSMatt Ranostay } 1093aff268cdSMatt Ranostay 1094aff268cdSMatt Ranostay static int apds9960_runtime_resume(struct device *dev) 1095aff268cdSMatt Ranostay { 1096aff268cdSMatt Ranostay struct apds9960_data *data = 1097aff268cdSMatt Ranostay iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 1098aff268cdSMatt Ranostay 1099aff268cdSMatt Ranostay return apds9960_set_powermode(data, 1); 1100aff268cdSMatt Ranostay } 1101aff268cdSMatt Ranostay #endif 1102aff268cdSMatt Ranostay 1103aff268cdSMatt Ranostay static const struct dev_pm_ops apds9960_pm_ops = { 1104d454ae2eSMatt Ranostay SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1105d454ae2eSMatt Ranostay pm_runtime_force_resume) 1106aff268cdSMatt Ranostay SET_RUNTIME_PM_OPS(apds9960_runtime_suspend, 1107aff268cdSMatt Ranostay apds9960_runtime_resume, NULL) 1108aff268cdSMatt Ranostay }; 1109aff268cdSMatt Ranostay 1110aff268cdSMatt Ranostay static const struct i2c_device_id apds9960_id[] = { 1111aff268cdSMatt Ranostay { "apds9960", 0 }, 1112aff268cdSMatt Ranostay {} 1113aff268cdSMatt Ranostay }; 1114aff268cdSMatt Ranostay MODULE_DEVICE_TABLE(i2c, apds9960_id); 1115aff268cdSMatt Ranostay 1116ae4e825bSJavier Martinez Canillas static const struct of_device_id apds9960_of_match[] = { 1117ae4e825bSJavier Martinez Canillas { .compatible = "avago,apds9960" }, 1118ae4e825bSJavier Martinez Canillas { } 1119ae4e825bSJavier Martinez Canillas }; 1120ae4e825bSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, apds9960_of_match); 1121ae4e825bSJavier Martinez Canillas 1122aff268cdSMatt Ranostay static struct i2c_driver apds9960_driver = { 1123aff268cdSMatt Ranostay .driver = { 1124aff268cdSMatt Ranostay .name = APDS9960_DRV_NAME, 1125ae4e825bSJavier Martinez Canillas .of_match_table = apds9960_of_match, 1126aff268cdSMatt Ranostay .pm = &apds9960_pm_ops, 1127aff268cdSMatt Ranostay }, 1128aff268cdSMatt Ranostay .probe = apds9960_probe, 1129aff268cdSMatt Ranostay .remove = apds9960_remove, 1130aff268cdSMatt Ranostay .id_table = apds9960_id, 1131aff268cdSMatt Ranostay }; 1132aff268cdSMatt Ranostay module_i2c_driver(apds9960_driver); 1133aff268cdSMatt Ranostay 1134d6ad8058SMatt Ranostay MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); 113550e5bae0SChristophe JAILLET MODULE_DESCRIPTION("APDS9960 Gesture/RGB/ALS/Proximity sensor"); 1136aff268cdSMatt Ranostay MODULE_LICENSE("GPL"); 1137