xref: /linux/drivers/iio/proximity/cros_ec_mkbp_proximity.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
17792225bSStephen Boyd // SPDX-License-Identifier: GPL-2.0
27792225bSStephen Boyd /*
37792225bSStephen Boyd  * Driver for cros-ec proximity sensor exposed through MKBP switch
47792225bSStephen Boyd  *
57792225bSStephen Boyd  * Copyright 2021 Google LLC.
67792225bSStephen Boyd  */
77792225bSStephen Boyd 
87792225bSStephen Boyd #include <linux/kernel.h>
9dae65fc2SAndy Shevchenko #include <linux/mod_devicetable.h>
107792225bSStephen Boyd #include <linux/module.h>
117792225bSStephen Boyd #include <linux/mutex.h>
127792225bSStephen Boyd #include <linux/notifier.h>
137792225bSStephen Boyd #include <linux/platform_device.h>
147792225bSStephen Boyd #include <linux/slab.h>
157792225bSStephen Boyd #include <linux/types.h>
167792225bSStephen Boyd 
177792225bSStephen Boyd #include <linux/platform_data/cros_ec_commands.h>
187792225bSStephen Boyd #include <linux/platform_data/cros_ec_proto.h>
197792225bSStephen Boyd 
207792225bSStephen Boyd #include <linux/iio/events.h>
217792225bSStephen Boyd #include <linux/iio/iio.h>
227792225bSStephen Boyd #include <linux/iio/sysfs.h>
237792225bSStephen Boyd 
24*5f60d5f6SAl Viro #include <linux/unaligned.h>
257792225bSStephen Boyd 
267792225bSStephen Boyd struct cros_ec_mkbp_proximity_data {
277792225bSStephen Boyd 	struct cros_ec_device *ec;
287792225bSStephen Boyd 	struct iio_dev *indio_dev;
297792225bSStephen Boyd 	struct mutex lock;
307792225bSStephen Boyd 	struct notifier_block notifier;
317792225bSStephen Boyd 	int last_proximity;
327792225bSStephen Boyd 	bool enabled;
337792225bSStephen Boyd };
347792225bSStephen Boyd 
357792225bSStephen Boyd static const struct iio_event_spec cros_ec_mkbp_proximity_events[] = {
367792225bSStephen Boyd 	{
377792225bSStephen Boyd 		.type = IIO_EV_TYPE_THRESH,
387792225bSStephen Boyd 		.dir = IIO_EV_DIR_EITHER,
397792225bSStephen Boyd 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
407792225bSStephen Boyd 	},
417792225bSStephen Boyd };
427792225bSStephen Boyd 
437792225bSStephen Boyd static const struct iio_chan_spec cros_ec_mkbp_proximity_chan_spec[] = {
447792225bSStephen Boyd 	{
457792225bSStephen Boyd 		.type = IIO_PROXIMITY,
467792225bSStephen Boyd 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
477792225bSStephen Boyd 		.event_spec = cros_ec_mkbp_proximity_events,
487792225bSStephen Boyd 		.num_event_specs = ARRAY_SIZE(cros_ec_mkbp_proximity_events),
497792225bSStephen Boyd 	},
507792225bSStephen Boyd };
517792225bSStephen Boyd 
cros_ec_mkbp_proximity_parse_state(const void * data)527792225bSStephen Boyd static int cros_ec_mkbp_proximity_parse_state(const void *data)
537792225bSStephen Boyd {
547792225bSStephen Boyd 	u32 switches = get_unaligned_le32(data);
557792225bSStephen Boyd 
567792225bSStephen Boyd 	return !!(switches & BIT(EC_MKBP_FRONT_PROXIMITY));
577792225bSStephen Boyd }
587792225bSStephen Boyd 
cros_ec_mkbp_proximity_query(struct cros_ec_device * ec_dev,int * state)597792225bSStephen Boyd static int cros_ec_mkbp_proximity_query(struct cros_ec_device *ec_dev,
607792225bSStephen Boyd 					int *state)
617792225bSStephen Boyd {
627792225bSStephen Boyd 	struct {
637792225bSStephen Boyd 		struct cros_ec_command msg;
647792225bSStephen Boyd 		union {
657792225bSStephen Boyd 			struct ec_params_mkbp_info params;
667792225bSStephen Boyd 			u32 switches;
677792225bSStephen Boyd 		};
687792225bSStephen Boyd 	} __packed buf = { };
697792225bSStephen Boyd 	struct ec_params_mkbp_info *params = &buf.params;
707792225bSStephen Boyd 	struct cros_ec_command *msg = &buf.msg;
717792225bSStephen Boyd 	u32 *switches = &buf.switches;
727792225bSStephen Boyd 	size_t insize = sizeof(*switches);
737792225bSStephen Boyd 	int ret;
747792225bSStephen Boyd 
757792225bSStephen Boyd 	msg->command = EC_CMD_MKBP_INFO;
767792225bSStephen Boyd 	msg->version = 1;
777792225bSStephen Boyd 	msg->outsize = sizeof(*params);
787792225bSStephen Boyd 	msg->insize = insize;
797792225bSStephen Boyd 
807792225bSStephen Boyd 	params->info_type = EC_MKBP_INFO_CURRENT;
817792225bSStephen Boyd 	params->event_type = EC_MKBP_EVENT_SWITCH;
827792225bSStephen Boyd 
837792225bSStephen Boyd 	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
847792225bSStephen Boyd 	if (ret < 0)
857792225bSStephen Boyd 		return ret;
867792225bSStephen Boyd 
877792225bSStephen Boyd 	if (ret != insize) {
887792225bSStephen Boyd 		dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n", ret,
897792225bSStephen Boyd 			 insize);
907792225bSStephen Boyd 		return -EPROTO;
917792225bSStephen Boyd 	}
927792225bSStephen Boyd 
937792225bSStephen Boyd 	*state = cros_ec_mkbp_proximity_parse_state(switches);
947792225bSStephen Boyd 	return IIO_VAL_INT;
957792225bSStephen Boyd }
967792225bSStephen Boyd 
cros_ec_mkbp_proximity_push_event(struct cros_ec_mkbp_proximity_data * data,int state)977792225bSStephen Boyd static void cros_ec_mkbp_proximity_push_event(struct cros_ec_mkbp_proximity_data *data, int state)
987792225bSStephen Boyd {
997792225bSStephen Boyd 	s64 timestamp;
1007792225bSStephen Boyd 	u64 ev;
1017792225bSStephen Boyd 	int dir;
1027792225bSStephen Boyd 	struct iio_dev *indio_dev = data->indio_dev;
1037792225bSStephen Boyd 	struct cros_ec_device *ec = data->ec;
1047792225bSStephen Boyd 
1057792225bSStephen Boyd 	mutex_lock(&data->lock);
1067792225bSStephen Boyd 	if (state != data->last_proximity) {
1077792225bSStephen Boyd 		if (data->enabled) {
1087792225bSStephen Boyd 			timestamp = ktime_to_ns(ec->last_event_time);
1097792225bSStephen Boyd 			if (iio_device_get_clock(indio_dev) != CLOCK_BOOTTIME)
1107792225bSStephen Boyd 				timestamp = iio_get_time_ns(indio_dev);
1117792225bSStephen Boyd 
1127792225bSStephen Boyd 			dir = state ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
1137792225bSStephen Boyd 			ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
1147792225bSStephen Boyd 						  IIO_EV_TYPE_THRESH, dir);
1157792225bSStephen Boyd 			iio_push_event(indio_dev, ev, timestamp);
1167792225bSStephen Boyd 		}
1177792225bSStephen Boyd 		data->last_proximity = state;
1187792225bSStephen Boyd 	}
1197792225bSStephen Boyd 	mutex_unlock(&data->lock);
1207792225bSStephen Boyd }
1217792225bSStephen Boyd 
cros_ec_mkbp_proximity_notify(struct notifier_block * nb,unsigned long queued_during_suspend,void * _ec)1227792225bSStephen Boyd static int cros_ec_mkbp_proximity_notify(struct notifier_block *nb,
1237792225bSStephen Boyd 					 unsigned long queued_during_suspend,
1247792225bSStephen Boyd 					 void *_ec)
1257792225bSStephen Boyd {
1267792225bSStephen Boyd 	struct cros_ec_mkbp_proximity_data *data;
1277792225bSStephen Boyd 	struct cros_ec_device *ec = _ec;
1287792225bSStephen Boyd 	u8 event_type = ec->event_data.event_type & EC_MKBP_EVENT_TYPE_MASK;
1297792225bSStephen Boyd 	void *switches;
1307792225bSStephen Boyd 	int state;
1317792225bSStephen Boyd 
1327792225bSStephen Boyd 	if (event_type == EC_MKBP_EVENT_SWITCH) {
1337792225bSStephen Boyd 		data = container_of(nb, struct cros_ec_mkbp_proximity_data,
1347792225bSStephen Boyd 				    notifier);
1357792225bSStephen Boyd 
1367792225bSStephen Boyd 		switches = &ec->event_data.data.switches;
1377792225bSStephen Boyd 		state = cros_ec_mkbp_proximity_parse_state(switches);
1387792225bSStephen Boyd 		cros_ec_mkbp_proximity_push_event(data, state);
1397792225bSStephen Boyd 	}
1407792225bSStephen Boyd 
1417792225bSStephen Boyd 	return NOTIFY_OK;
1427792225bSStephen Boyd }
1437792225bSStephen Boyd 
cros_ec_mkbp_proximity_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long mask)1447792225bSStephen Boyd static int cros_ec_mkbp_proximity_read_raw(struct iio_dev *indio_dev,
1457792225bSStephen Boyd 			   const struct iio_chan_spec *chan, int *val,
1467792225bSStephen Boyd 			   int *val2, long mask)
1477792225bSStephen Boyd {
1487792225bSStephen Boyd 	struct cros_ec_mkbp_proximity_data *data = iio_priv(indio_dev);
1497792225bSStephen Boyd 	struct cros_ec_device *ec = data->ec;
1507792225bSStephen Boyd 
1517792225bSStephen Boyd 	if (chan->type == IIO_PROXIMITY && mask == IIO_CHAN_INFO_RAW)
1527792225bSStephen Boyd 		return cros_ec_mkbp_proximity_query(ec, val);
1537792225bSStephen Boyd 
1547792225bSStephen Boyd 	return -EINVAL;
1557792225bSStephen Boyd }
1567792225bSStephen Boyd 
cros_ec_mkbp_proximity_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)1577792225bSStephen Boyd static int cros_ec_mkbp_proximity_read_event_config(struct iio_dev *indio_dev,
1587792225bSStephen Boyd 				    const struct iio_chan_spec *chan,
1597792225bSStephen Boyd 				    enum iio_event_type type,
1607792225bSStephen Boyd 				    enum iio_event_direction dir)
1617792225bSStephen Boyd {
1627792225bSStephen Boyd 	struct cros_ec_mkbp_proximity_data *data = iio_priv(indio_dev);
1637792225bSStephen Boyd 
1647792225bSStephen Boyd 	return data->enabled;
1657792225bSStephen Boyd }
1667792225bSStephen Boyd 
cros_ec_mkbp_proximity_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)1677792225bSStephen Boyd static int cros_ec_mkbp_proximity_write_event_config(struct iio_dev *indio_dev,
1687792225bSStephen Boyd 				     const struct iio_chan_spec *chan,
1697792225bSStephen Boyd 				     enum iio_event_type type,
1707792225bSStephen Boyd 				     enum iio_event_direction dir, int state)
1717792225bSStephen Boyd {
1727792225bSStephen Boyd 	struct cros_ec_mkbp_proximity_data *data = iio_priv(indio_dev);
1737792225bSStephen Boyd 
1747792225bSStephen Boyd 	mutex_lock(&data->lock);
1757792225bSStephen Boyd 	data->enabled = state;
1767792225bSStephen Boyd 	mutex_unlock(&data->lock);
1777792225bSStephen Boyd 
1787792225bSStephen Boyd 	return 0;
1797792225bSStephen Boyd }
1807792225bSStephen Boyd 
1817792225bSStephen Boyd static const struct iio_info cros_ec_mkbp_proximity_info = {
1827792225bSStephen Boyd 	.read_raw = cros_ec_mkbp_proximity_read_raw,
1837792225bSStephen Boyd 	.read_event_config = cros_ec_mkbp_proximity_read_event_config,
1847792225bSStephen Boyd 	.write_event_config = cros_ec_mkbp_proximity_write_event_config,
1857792225bSStephen Boyd };
1867792225bSStephen Boyd 
cros_ec_mkbp_proximity_resume(struct device * dev)187dc69c610SJonathan Cameron static int cros_ec_mkbp_proximity_resume(struct device *dev)
1887792225bSStephen Boyd {
1897792225bSStephen Boyd 	struct cros_ec_mkbp_proximity_data *data = dev_get_drvdata(dev);
1907792225bSStephen Boyd 	struct cros_ec_device *ec = data->ec;
1917792225bSStephen Boyd 	int ret, state;
1927792225bSStephen Boyd 
1937792225bSStephen Boyd 	ret = cros_ec_mkbp_proximity_query(ec, &state);
1947792225bSStephen Boyd 	if (ret < 0) {
1957792225bSStephen Boyd 		dev_warn(dev, "failed to fetch proximity state on resume: %d\n",
1967792225bSStephen Boyd 			 ret);
1977792225bSStephen Boyd 	} else {
1987792225bSStephen Boyd 		cros_ec_mkbp_proximity_push_event(data, state);
1997792225bSStephen Boyd 	}
2007792225bSStephen Boyd 
2017792225bSStephen Boyd 	return 0;
2027792225bSStephen Boyd }
2037792225bSStephen Boyd 
204dc69c610SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(cros_ec_mkbp_proximity_pm_ops, NULL,
2057792225bSStephen Boyd 				cros_ec_mkbp_proximity_resume);
2067792225bSStephen Boyd 
cros_ec_mkbp_proximity_probe(struct platform_device * pdev)2077792225bSStephen Boyd static int cros_ec_mkbp_proximity_probe(struct platform_device *pdev)
2087792225bSStephen Boyd {
2097792225bSStephen Boyd 	struct device *dev = &pdev->dev;
2107792225bSStephen Boyd 	struct cros_ec_device *ec = dev_get_drvdata(dev->parent);
2117792225bSStephen Boyd 	struct iio_dev *indio_dev;
2127792225bSStephen Boyd 	struct cros_ec_mkbp_proximity_data *data;
2137792225bSStephen Boyd 	int ret;
2147792225bSStephen Boyd 
2157792225bSStephen Boyd 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
2167792225bSStephen Boyd 	if (!indio_dev)
2177792225bSStephen Boyd 		return -ENOMEM;
2187792225bSStephen Boyd 
2197792225bSStephen Boyd 	data = iio_priv(indio_dev);
2207792225bSStephen Boyd 	data->ec = ec;
2217792225bSStephen Boyd 	data->indio_dev = indio_dev;
2227792225bSStephen Boyd 	data->last_proximity = -1; /* Unknown to start */
2237792225bSStephen Boyd 	mutex_init(&data->lock);
2247792225bSStephen Boyd 	platform_set_drvdata(pdev, data);
2257792225bSStephen Boyd 
2267792225bSStephen Boyd 	indio_dev->name = dev->driver->name;
2277792225bSStephen Boyd 	indio_dev->info = &cros_ec_mkbp_proximity_info;
2287792225bSStephen Boyd 	indio_dev->modes = INDIO_DIRECT_MODE;
2297792225bSStephen Boyd 	indio_dev->channels = cros_ec_mkbp_proximity_chan_spec;
2307792225bSStephen Boyd 	indio_dev->num_channels = ARRAY_SIZE(cros_ec_mkbp_proximity_chan_spec);
2317792225bSStephen Boyd 
2327792225bSStephen Boyd 	ret = devm_iio_device_register(dev, indio_dev);
2337792225bSStephen Boyd 	if (ret)
2347792225bSStephen Boyd 		return ret;
2357792225bSStephen Boyd 
2367792225bSStephen Boyd 	data->notifier.notifier_call = cros_ec_mkbp_proximity_notify;
2377792225bSStephen Boyd 	blocking_notifier_chain_register(&ec->event_notifier, &data->notifier);
2387792225bSStephen Boyd 
2397792225bSStephen Boyd 	return 0;
2407792225bSStephen Boyd }
2417792225bSStephen Boyd 
cros_ec_mkbp_proximity_remove(struct platform_device * pdev)2422df694f7SUwe Kleine-König static void cros_ec_mkbp_proximity_remove(struct platform_device *pdev)
2437792225bSStephen Boyd {
2447792225bSStephen Boyd 	struct cros_ec_mkbp_proximity_data *data = platform_get_drvdata(pdev);
2457792225bSStephen Boyd 	struct cros_ec_device *ec = data->ec;
2467792225bSStephen Boyd 
2477792225bSStephen Boyd 	blocking_notifier_chain_unregister(&ec->event_notifier,
2487792225bSStephen Boyd 					   &data->notifier);
2497792225bSStephen Boyd }
2507792225bSStephen Boyd 
2517792225bSStephen Boyd static const struct of_device_id cros_ec_mkbp_proximity_of_match[] = {
2527792225bSStephen Boyd 	{ .compatible = "google,cros-ec-mkbp-proximity" },
2537792225bSStephen Boyd 	{}
2547792225bSStephen Boyd };
2557792225bSStephen Boyd MODULE_DEVICE_TABLE(of, cros_ec_mkbp_proximity_of_match);
2567792225bSStephen Boyd 
2577792225bSStephen Boyd static struct platform_driver cros_ec_mkbp_proximity_driver = {
2587792225bSStephen Boyd 	.driver = {
2597792225bSStephen Boyd 		.name = "cros-ec-mkbp-proximity",
2607792225bSStephen Boyd 		.of_match_table = cros_ec_mkbp_proximity_of_match,
261dc69c610SJonathan Cameron 		.pm = pm_sleep_ptr(&cros_ec_mkbp_proximity_pm_ops),
2627792225bSStephen Boyd 	},
2637792225bSStephen Boyd 	.probe = cros_ec_mkbp_proximity_probe,
2642df694f7SUwe Kleine-König 	.remove_new = cros_ec_mkbp_proximity_remove,
2657792225bSStephen Boyd };
2667792225bSStephen Boyd module_platform_driver(cros_ec_mkbp_proximity_driver);
2677792225bSStephen Boyd 
2687792225bSStephen Boyd MODULE_LICENSE("GPL v2");
2697792225bSStephen Boyd MODULE_DESCRIPTION("ChromeOS EC MKBP proximity sensor driver");
270