History

mfd / platform / iio: cros_ec: Register sensor through sensorhub

Remove the duplicated code in MFD, since MFD just registers cros-ec-sensorhub if at least one sensor is present. Change IIO cros-ec driver to get the pointer to the cros-ec-dev through cros-ec-sensorhub.

Gwendal Grignou committed 6 years ago

Commit d60ac881 parent 53067477 files · +23 −218Browse files

Unified · Split

7 files
  1. drivers/iio/accel/cros_ec_accel_legacy.c +0 −6
  2. drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c +0 −6
  3. drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 −1
  4. drivers/iio/light/cros_ec_light_prox.c +0 −6
  5. drivers/mfd/cros_ec_dev.c +12 −191
  6. include/linux/platform_data/cros_ec_proto.h +0 −8
  7. include/linux/platform_data/cros_ec_sensorhub.h +8 −0
drivers/iio/accel/cros_ec_accel_legacy.c+0−6
@@ -163,16 +163,10 @@ static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = { · 162 unmodified lines
163static int cros_ec_accel_legacy_probe(struct platform_device *pdev)163static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
164{164{
165 struct device *dev = &pdev->dev;165 struct device *dev = &pdev->dev;
166 struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
167 struct iio_dev *indio_dev;166 struct iio_dev *indio_dev;
168 struct cros_ec_sensors_core_state *state;167 struct cros_ec_sensors_core_state *state;
169 int ret;168 int ret;
170 169
171 if (!ec || !ec->ec_dev) {
172 dev_warn(&pdev->dev, "No EC device found.\n");
173 return -EINVAL;
174 }
175
176 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));170 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
177 if (!indio_dev)171 if (!indio_dev)
178 return -ENOMEM;172 return -ENOMEM;
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c+0−6
@@ -222,17 +222,11 @@ static const struct iio_info ec_sensors_info = { · 221 unmodified lines
222static int cros_ec_sensors_probe(struct platform_device *pdev)222static int cros_ec_sensors_probe(struct platform_device *pdev)
223{223{
224 struct device *dev = &pdev->dev;224 struct device *dev = &pdev->dev;
225 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
226 struct iio_dev *indio_dev;225 struct iio_dev *indio_dev;
227 struct cros_ec_sensors_state *state;226 struct cros_ec_sensors_state *state;
228 struct iio_chan_spec *channel;227 struct iio_chan_spec *channel;
229 int ret, i;228 int ret, i;
230 229
231 if (!ec_dev || !ec_dev->ec_dev) {
232 dev_warn(&pdev->dev, "No CROS EC device found.\n");
233 return -EINVAL;
234 }
235
236 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));230 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
237 if (!indio_dev)231 if (!indio_dev)
238 return -ENOMEM;232 return -ENOMEM;
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c+3−1
@@ -18,6 +18,7 @@ · 17 unmodified lines
18#include <linux/slab.h>18#include <linux/slab.h>
19#include <linux/platform_data/cros_ec_commands.h>19#include <linux/platform_data/cros_ec_commands.h>
20#include <linux/platform_data/cros_ec_proto.h>20#include <linux/platform_data/cros_ec_proto.h>
21#include <linux/platform_data/cros_ec_sensorhub.h>
21#include <linux/platform_device.h>22#include <linux/platform_device.h>
22 23
23static char *cros_ec_loc[] = {24static char *cros_ec_loc[] = {
@@ -88,7 +89,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev, · 64 unmodified lines
88{89{
89 struct device *dev = &pdev->dev;90 struct device *dev = &pdev->dev;
90 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);91 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
91 struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);92 struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
93 struct cros_ec_dev *ec = sensor_hub->ec;
92 struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);94 struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
93 u32 ver_mask;95 u32 ver_mask;
94 int ret, i;96 int ret, i;
drivers/iio/light/cros_ec_light_prox.c+0−6
@@ -169,17 +169,11 @@ static const struct iio_info cros_ec_light_prox_info = { · 168 unmodified lines
169static int cros_ec_light_prox_probe(struct platform_device *pdev)169static int cros_ec_light_prox_probe(struct platform_device *pdev)
170{170{
171 struct device *dev = &pdev->dev;171 struct device *dev = &pdev->dev;
172 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
173 struct iio_dev *indio_dev;172 struct iio_dev *indio_dev;
174 struct cros_ec_light_prox_state *state;173 struct cros_ec_light_prox_state *state;
175 struct iio_chan_spec *channel;174 struct iio_chan_spec *channel;
176 int ret;175 int ret;
177 176
178 if (!ec_dev || !ec_dev->ec_dev) {
179 dev_warn(dev, "No CROS EC device found.\n");
180 return -EINVAL;
181 }
182
183 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));177 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
184 if (!indio_dev)178 if (!indio_dev)
185 return -ENOMEM;179 return -ENOMEM;
drivers/mfd/cros_ec_dev.c+12−191
@@ -78,6 +78,10 @@ static const struct mfd_cell cros_ec_rtc_cells[] = { · 77 unmodified lines
78 { .name = "cros-ec-rtc", },78 { .name = "cros-ec-rtc", },
79};79};
80 80
81static const struct mfd_cell cros_ec_sensorhub_cells[] = {
82 { .name = "cros-ec-sensorhub", },
83};
84
81static const struct mfd_cell cros_usbpd_charger_cells[] = {85static const struct mfd_cell cros_usbpd_charger_cells[] = {
82 { .name = "cros-usbpd-charger", },86 { .name = "cros-usbpd-charger", },
83 { .name = "cros-usbpd-logger", },87 { .name = "cros-usbpd-logger", },
@@ -117,192 +121,6 @@ static void cros_ec_class_release(struct device *dev) · 33 unmodified lines
117 kfree(to_cros_ec_dev(dev));121 kfree(to_cros_ec_dev(dev));
118}122}
119 123
120static void cros_ec_sensors_register(struct cros_ec_dev *ec)
121{
122 /*
123 * Issue a command to get the number of sensor reported.
124 * Build an array of sensors driver and register them all.
125 */
126 int ret, i, id, sensor_num;
127 struct mfd_cell *sensor_cells;
128 struct cros_ec_sensor_platform *sensor_platforms;
129 int sensor_type[MOTIONSENSE_TYPE_MAX];
130 struct ec_params_motion_sense *params;
131 struct ec_response_motion_sense *resp;
132 struct cros_ec_command *msg;
133
134 msg = kzalloc(sizeof(struct cros_ec_command) +
135 max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
136 if (msg == NULL)
137 return;
138
139 msg->version = 2;
140 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
141 msg->outsize = sizeof(*params);
142 msg->insize = sizeof(*resp);
143
144 params = (struct ec_params_motion_sense *)msg->data;
145 params->cmd = MOTIONSENSE_CMD_DUMP;
146
147 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
148 if (ret < 0) {
149 dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
150 ret, msg->result);
151 goto error;
152 }
153
154 resp = (struct ec_response_motion_sense *)msg->data;
155 sensor_num = resp->dump.sensor_count;
156 /*
157 * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
158 */
159 sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
160 GFP_KERNEL);
161 if (sensor_cells == NULL)
162 goto error;
163
164 sensor_platforms = kcalloc(sensor_num,
165 sizeof(struct cros_ec_sensor_platform),
166 GFP_KERNEL);
167 if (sensor_platforms == NULL)
168 goto error_platforms;
169
170 memset(sensor_type, 0, sizeof(sensor_type));
171 id = 0;
172 for (i = 0; i < sensor_num; i++) {
173 params->cmd = MOTIONSENSE_CMD_INFO;
174 params->info.sensor_num = i;
175 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
176 if (ret < 0) {
177 dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
178 i, ret, msg->result);
179 continue;
180 }
181 switch (resp->info.type) {
182 case MOTIONSENSE_TYPE_ACCEL:
183 sensor_cells[id].name = "cros-ec-accel";
184 break;
185 case MOTIONSENSE_TYPE_BARO:
186 sensor_cells[id].name = "cros-ec-baro";
187 break;
188 case MOTIONSENSE_TYPE_GYRO:
189 sensor_cells[id].name = "cros-ec-gyro";
190 break;
191 case MOTIONSENSE_TYPE_MAG:
192 sensor_cells[id].name = "cros-ec-mag";
193 break;
194 case MOTIONSENSE_TYPE_PROX:
195 sensor_cells[id].name = "cros-ec-prox";
196 break;
197 case MOTIONSENSE_TYPE_LIGHT:
198 sensor_cells[id].name = "cros-ec-light";
199 break;
200 case MOTIONSENSE_TYPE_ACTIVITY:
201 sensor_cells[id].name = "cros-ec-activity";
202 break;
203 default:
204 dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
205 continue;
206 }
207 sensor_platforms[id].sensor_num = i;
208 sensor_cells[id].id = sensor_type[resp->info.type];
209 sensor_cells[id].platform_data = &sensor_platforms[id];
210 sensor_cells[id].pdata_size =
211 sizeof(struct cros_ec_sensor_platform);
212
213 sensor_type[resp->info.type]++;
214 id++;
215 }
216
217 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
218 ec->has_kb_wake_angle = true;
219
220 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
221 sensor_cells[id].name = "cros-ec-ring";
222 id++;
223 }
224 if (cros_ec_check_features(ec,
225 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
226 sensor_cells[id].name = "cros-ec-lid-angle";
227 id++;
228 }
229
230 ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
231 NULL, 0, NULL);
232 if (ret)
233 dev_err(ec->dev, "failed to add EC sensors\n");
234
235 kfree(sensor_platforms);
236error_platforms:
237 kfree(sensor_cells);
238error:
239 kfree(msg);
240}
241
242static struct cros_ec_sensor_platform sensor_platforms[] = {
243 { .sensor_num = 0 },
244 { .sensor_num = 1 }
245};
246
247static const struct mfd_cell cros_ec_accel_legacy_cells[] = {
248 {
249 .name = "cros-ec-accel-legacy",
250 .platform_data = &sensor_platforms[0],
251 .pdata_size = sizeof(struct cros_ec_sensor_platform),
252 },
253 {
254 .name = "cros-ec-accel-legacy",
255 .platform_data = &sensor_platforms[1],
256 .pdata_size = sizeof(struct cros_ec_sensor_platform),
257 }
258};
259
260static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
261{
262 struct cros_ec_device *ec_dev = ec->ec_dev;
263 u8 status;
264 int ret;
265
266 /*
267 * ECs that need legacy support are the main EC, directly connected to
268 * the AP.
269 */
270 if (ec->cmd_offset != 0)
271 return;
272
273 /*
274 * Check if EC supports direct memory reads and if EC has
275 * accelerometers.
276 */
277 if (ec_dev->cmd_readmem) {
278 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS, 1,
279 &status);
280 if (ret < 0) {
281 dev_warn(ec->dev, "EC direct read error.\n");
282 return;
283 }
284
285 /* Check if EC has accelerometers. */
286 if (!(status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
287 dev_info(ec->dev, "EC does not have accelerometers.\n");
288 return;
289 }
290 }
291
292 /*
293 * The device may still support accelerometers:
294 * it would be an older ARM based device that do not suppor the
295 * EC_CMD_GET_FEATURES command.
296 *
297 * Register 2 accelerometers, we will fail in the IIO driver if there
298 * are no sensors.
299 */
300 ret = mfd_add_hotplug_devices(ec->dev, cros_ec_accel_legacy_cells,
301 ARRAY_SIZE(cros_ec_accel_legacy_cells));
302 if (ret)
303 dev_err(ec_dev->dev, "failed to add EC sensors\n");
304}
305
306static int ec_device_probe(struct platform_device *pdev)124static int ec_device_probe(struct platform_device *pdev)
307{125{
308 int retval = -ENOMEM;126 int retval = -ENOMEM;
@@ -358,11 +176,14 @@ static int ec_device_probe(struct platform_device *pdev) · 49 unmodified lines
358 goto failed;176 goto failed;
359 177
360 /* check whether this EC is a sensor hub. */178 /* check whether this EC is a sensor hub. */
361 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))179 if (cros_ec_get_sensor_count(ec) > 0) {
362 cros_ec_sensors_register(ec);180 retval = mfd_add_hotplug_devices(ec->dev,
363 else181 cros_ec_sensorhub_cells,
364 /* Workaroud for older EC firmware */182 ARRAY_SIZE(cros_ec_sensorhub_cells));
365 cros_ec_accel_legacy_register(ec);183 if (retval)
184 dev_err(ec->dev, "failed to add %s subdevice: %d\n",
185 cros_ec_sensorhub_cells->name, retval);
186 }
366 187
367 /*188 /*
368 * The following subdevices can be detected by sending the189 * The following subdevices can be detected by sending the
include/linux/platform_data/cros_ec_proto.h+0−8
@@ -168,14 +168,6 @@ struct cros_ec_device { · 167 unmodified lines
168 struct platform_device *pd;168 struct platform_device *pd;
169};169};
170 170
171/**
172 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
173 * @sensor_num: Id of the sensor, as reported by the EC.
174 */
175struct cros_ec_sensor_platform {
176 u8 sensor_num;
177};
178
179/**171/**
180 * struct cros_ec_platform - ChromeOS EC platform information.172 * struct cros_ec_platform - ChromeOS EC platform information.
181 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)173 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
include/linux/platform_data/cros_ec_sensorhub.h+8−0
@@ -10,6 +10,14 @@ · 9 unmodified lines
10 10
11#include <linux/platform_data/cros_ec_commands.h>11#include <linux/platform_data/cros_ec_commands.h>
12 12
13/**
14 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
15 * @sensor_num: Id of the sensor, as reported by the EC.
16 */
17struct cros_ec_sensor_platform {
18 u8 sensor_num;
19};
20
13/**21/**
14 * struct cros_ec_sensorhub - Sensor Hub device data.22 * struct cros_ec_sensorhub - Sensor Hub device data.
15 *23 *

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t