| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * cros_ec_light_prox - Driver for light and prox sensors behing CrosEC. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2017 Google, Inc |
|---|
| 5 | | - * |
|---|
| 6 | | - * This software is licensed under the terms of the GNU General Public |
|---|
| 7 | | - * License version 2, as published by the Free Software Foundation, and |
|---|
| 8 | | - * may be copied, distributed, and modified under those terms. |
|---|
| 9 | | - * |
|---|
| 10 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 11 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | | - * GNU General Public License for more details. |
|---|
| 14 | 6 | */ |
|---|
| 15 | 7 | |
|---|
| 16 | | -#include <linux/delay.h> |
|---|
| 17 | 8 | #include <linux/device.h> |
|---|
| 18 | 9 | #include <linux/iio/buffer.h> |
|---|
| 19 | 10 | #include <linux/iio/common/cros_ec_sensors_core.h> |
|---|
| .. | .. |
|---|
| 23 | 14 | #include <linux/iio/triggered_buffer.h> |
|---|
| 24 | 15 | #include <linux/iio/trigger_consumer.h> |
|---|
| 25 | 16 | #include <linux/kernel.h> |
|---|
| 26 | | -#include <linux/mfd/cros_ec.h> |
|---|
| 27 | | -#include <linux/mfd/cros_ec_commands.h> |
|---|
| 28 | 17 | #include <linux/module.h> |
|---|
| 18 | +#include <linux/platform_data/cros_ec_commands.h> |
|---|
| 19 | +#include <linux/platform_data/cros_ec_proto.h> |
|---|
| 29 | 20 | #include <linux/platform_device.h> |
|---|
| 30 | 21 | #include <linux/slab.h> |
|---|
| 31 | | -#include <linux/sysfs.h> |
|---|
| 32 | 22 | |
|---|
| 33 | 23 | /* |
|---|
| 34 | 24 | * We only represent one entry for light or proximity. EC is merging different |
|---|
| .. | .. |
|---|
| 52 | 42 | struct cros_ec_light_prox_state *st = iio_priv(indio_dev); |
|---|
| 53 | 43 | u16 data = 0; |
|---|
| 54 | 44 | s64 val64; |
|---|
| 55 | | - int ret = IIO_VAL_INT; |
|---|
| 45 | + int ret; |
|---|
| 56 | 46 | int idx = chan->scan_index; |
|---|
| 57 | 47 | |
|---|
| 58 | 48 | mutex_lock(&st->core.cmd_lock); |
|---|
| .. | .. |
|---|
| 60 | 50 | switch (mask) { |
|---|
| 61 | 51 | case IIO_CHAN_INFO_RAW: |
|---|
| 62 | 52 | if (chan->type == IIO_PROXIMITY) { |
|---|
| 63 | | - if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx, |
|---|
| 64 | | - (s16 *)&data) < 0) { |
|---|
| 65 | | - ret = -EIO; |
|---|
| 53 | + ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx, |
|---|
| 54 | + (s16 *)&data); |
|---|
| 55 | + if (ret) |
|---|
| 66 | 56 | break; |
|---|
| 67 | | - } |
|---|
| 68 | 57 | *val = data; |
|---|
| 58 | + ret = IIO_VAL_INT; |
|---|
| 69 | 59 | } else { |
|---|
| 70 | 60 | ret = -EINVAL; |
|---|
| 71 | 61 | } |
|---|
| 72 | 62 | break; |
|---|
| 73 | 63 | case IIO_CHAN_INFO_PROCESSED: |
|---|
| 74 | 64 | if (chan->type == IIO_LIGHT) { |
|---|
| 75 | | - if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx, |
|---|
| 76 | | - (s16 *)&data) < 0) { |
|---|
| 77 | | - ret = -EIO; |
|---|
| 65 | + ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx, |
|---|
| 66 | + (s16 *)&data); |
|---|
| 67 | + if (ret) |
|---|
| 78 | 68 | break; |
|---|
| 79 | | - } |
|---|
| 80 | 69 | /* |
|---|
| 81 | 70 | * The data coming from the light sensor is |
|---|
| 82 | 71 | * pre-processed and represents the ambient light |
|---|
| .. | .. |
|---|
| 92 | 81 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET; |
|---|
| 93 | 82 | st->core.param.sensor_offset.flags = 0; |
|---|
| 94 | 83 | |
|---|
| 95 | | - if (cros_ec_motion_send_host_cmd(&st->core, 0)) { |
|---|
| 96 | | - ret = -EIO; |
|---|
| 84 | + ret = cros_ec_motion_send_host_cmd(&st->core, 0); |
|---|
| 85 | + if (ret) |
|---|
| 97 | 86 | break; |
|---|
| 98 | | - } |
|---|
| 99 | 87 | |
|---|
| 100 | 88 | /* Save values */ |
|---|
| 101 | | - st->core.calib[0] = st->core.resp->sensor_offset.offset[0]; |
|---|
| 89 | + st->core.calib[0].offset = |
|---|
| 90 | + st->core.resp->sensor_offset.offset[0]; |
|---|
| 102 | 91 | |
|---|
| 103 | | - *val = st->core.calib[idx]; |
|---|
| 92 | + *val = st->core.calib[idx].offset; |
|---|
| 93 | + ret = IIO_VAL_INT; |
|---|
| 104 | 94 | break; |
|---|
| 105 | 95 | case IIO_CHAN_INFO_CALIBSCALE: |
|---|
| 106 | 96 | /* |
|---|
| .. | .. |
|---|
| 111 | 101 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; |
|---|
| 112 | 102 | st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE; |
|---|
| 113 | 103 | |
|---|
| 114 | | - if (cros_ec_motion_send_host_cmd(&st->core, 0)) { |
|---|
| 115 | | - ret = -EIO; |
|---|
| 104 | + ret = cros_ec_motion_send_host_cmd(&st->core, 0); |
|---|
| 105 | + if (ret) |
|---|
| 116 | 106 | break; |
|---|
| 117 | | - } |
|---|
| 118 | 107 | |
|---|
| 119 | 108 | val64 = st->core.resp->sensor_range.ret; |
|---|
| 120 | 109 | *val = val64 >> 16; |
|---|
| .. | .. |
|---|
| 137 | 126 | int val, int val2, long mask) |
|---|
| 138 | 127 | { |
|---|
| 139 | 128 | struct cros_ec_light_prox_state *st = iio_priv(indio_dev); |
|---|
| 140 | | - int ret = 0; |
|---|
| 129 | + int ret; |
|---|
| 141 | 130 | int idx = chan->scan_index; |
|---|
| 142 | 131 | |
|---|
| 143 | 132 | mutex_lock(&st->core.cmd_lock); |
|---|
| 144 | 133 | |
|---|
| 145 | 134 | switch (mask) { |
|---|
| 146 | 135 | case IIO_CHAN_INFO_CALIBBIAS: |
|---|
| 147 | | - st->core.calib[idx] = val; |
|---|
| 136 | + st->core.calib[idx].offset = val; |
|---|
| 148 | 137 | /* Send to EC for each axis, even if not complete */ |
|---|
| 149 | 138 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET; |
|---|
| 150 | 139 | st->core.param.sensor_offset.flags = MOTION_SENSE_SET_OFFSET; |
|---|
| 151 | | - st->core.param.sensor_offset.offset[0] = st->core.calib[0]; |
|---|
| 140 | + st->core.param.sensor_offset.offset[0] = |
|---|
| 141 | + st->core.calib[0].offset; |
|---|
| 152 | 142 | st->core.param.sensor_offset.temp = |
|---|
| 153 | 143 | EC_MOTION_SENSE_INVALID_CALIB_TEMP; |
|---|
| 154 | | - if (cros_ec_motion_send_host_cmd(&st->core, 0)) |
|---|
| 155 | | - ret = -EIO; |
|---|
| 144 | + ret = cros_ec_motion_send_host_cmd(&st->core, 0); |
|---|
| 156 | 145 | break; |
|---|
| 157 | 146 | case IIO_CHAN_INFO_CALIBSCALE: |
|---|
| 158 | 147 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; |
|---|
| 159 | | - st->core.param.sensor_range.data = (val << 16) | (val2 / 100); |
|---|
| 160 | | - if (cros_ec_motion_send_host_cmd(&st->core, 0)) |
|---|
| 161 | | - ret = -EIO; |
|---|
| 148 | + st->core.curr_range = (val << 16) | (val2 / 100); |
|---|
| 149 | + st->core.param.sensor_range.data = st->core.curr_range; |
|---|
| 150 | + ret = cros_ec_motion_send_host_cmd(&st->core, 0); |
|---|
| 151 | + if (ret == 0) |
|---|
| 152 | + st->core.range_updated = true; |
|---|
| 162 | 153 | break; |
|---|
| 163 | 154 | default: |
|---|
| 164 | 155 | ret = cros_ec_sensors_core_write(&st->core, chan, val, val2, |
|---|
| .. | .. |
|---|
| 174 | 165 | static const struct iio_info cros_ec_light_prox_info = { |
|---|
| 175 | 166 | .read_raw = &cros_ec_light_prox_read, |
|---|
| 176 | 167 | .write_raw = &cros_ec_light_prox_write, |
|---|
| 168 | + .read_avail = &cros_ec_sensors_core_read_avail, |
|---|
| 177 | 169 | }; |
|---|
| 178 | 170 | |
|---|
| 179 | 171 | static int cros_ec_light_prox_probe(struct platform_device *pdev) |
|---|
| 180 | 172 | { |
|---|
| 181 | 173 | struct device *dev = &pdev->dev; |
|---|
| 182 | | - struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent); |
|---|
| 183 | 174 | struct iio_dev *indio_dev; |
|---|
| 184 | 175 | struct cros_ec_light_prox_state *state; |
|---|
| 185 | 176 | struct iio_chan_spec *channel; |
|---|
| 186 | 177 | int ret; |
|---|
| 187 | 178 | |
|---|
| 188 | | - if (!ec_dev || !ec_dev->ec_dev) { |
|---|
| 189 | | - dev_warn(dev, "No CROS EC device found.\n"); |
|---|
| 190 | | - return -EINVAL; |
|---|
| 191 | | - } |
|---|
| 192 | | - |
|---|
| 193 | 179 | indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); |
|---|
| 194 | 180 | if (!indio_dev) |
|---|
| 195 | 181 | return -ENOMEM; |
|---|
| 196 | 182 | |
|---|
| 197 | | - ret = cros_ec_sensors_core_init(pdev, indio_dev, true); |
|---|
| 183 | + ret = cros_ec_sensors_core_init(pdev, indio_dev, true, |
|---|
| 184 | + cros_ec_sensors_capture, |
|---|
| 185 | + cros_ec_sensors_push_data, |
|---|
| 186 | + true); |
|---|
| 198 | 187 | if (ret) |
|---|
| 199 | 188 | return ret; |
|---|
| 200 | 189 | |
|---|
| .. | .. |
|---|
| 206 | 195 | |
|---|
| 207 | 196 | /* Common part */ |
|---|
| 208 | 197 | channel->info_mask_shared_by_all = |
|---|
| 209 | | - BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
|---|
| 210 | | - BIT(IIO_CHAN_INFO_FREQUENCY); |
|---|
| 198 | + BIT(IIO_CHAN_INFO_SAMP_FREQ); |
|---|
| 199 | + channel->info_mask_shared_by_all_available = |
|---|
| 200 | + BIT(IIO_CHAN_INFO_SAMP_FREQ); |
|---|
| 211 | 201 | channel->scan_type.realbits = CROS_EC_SENSOR_BITS; |
|---|
| 212 | 202 | channel->scan_type.storagebits = CROS_EC_SENSOR_BITS; |
|---|
| 213 | 203 | channel->scan_type.shift = 0; |
|---|
| 214 | 204 | channel->scan_index = 0; |
|---|
| 215 | 205 | channel->ext_info = cros_ec_sensors_ext_info; |
|---|
| 216 | 206 | channel->scan_type.sign = 'u'; |
|---|
| 217 | | - |
|---|
| 218 | | - state->core.calib[0] = 0; |
|---|
| 219 | 207 | |
|---|
| 220 | 208 | /* Sensor specific */ |
|---|
| 221 | 209 | switch (state->core.type) { |
|---|
| .. | .. |
|---|
| 252 | 240 | indio_dev->num_channels = CROS_EC_LIGHT_PROX_MAX_CHANNELS; |
|---|
| 253 | 241 | |
|---|
| 254 | 242 | state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; |
|---|
| 255 | | - |
|---|
| 256 | | - ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, |
|---|
| 257 | | - cros_ec_sensors_capture, NULL); |
|---|
| 258 | | - if (ret) |
|---|
| 259 | | - return ret; |
|---|
| 260 | 243 | |
|---|
| 261 | 244 | return devm_iio_device_register(dev, indio_dev); |
|---|
| 262 | 245 | } |
|---|