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