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