Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * drivers/extcon/extcon-adc-jack.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Analog Jack extcon driver with ADC-based detection capability.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2016 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Chanwoo Choi <cw00.choi@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Copyright (C) 2012 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * MyungJoo Ham <myungjoo.ham@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Modified for calling to IIO to get adc by <anish.singh@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/extcon/extcon-adc-jack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/extcon-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * struct adc_jack_data - internal data for adc_jack device driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * @edev:		extcon device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * @cable_names:	list of supported cables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * @adc_conditions:	list of adc value conditions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * @num_conditions:	size of adc_conditions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * @irq:		irq number of attach/detach event (0 if not exist).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * @handling_delay:	interrupt handler will schedule extcon event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *			handling at handling_delay jiffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * @handler:		extcon event handler called by interrupt handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * @chan:		iio channel being queried.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct adc_jack_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct extcon_dev *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	const unsigned int **cable_names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct adc_jack_cond *adc_conditions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int num_conditions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	unsigned long handling_delay; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct delayed_work handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct iio_channel *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	bool wakeup_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static void adc_jack_handler(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct adc_jack_data *data = container_of(to_delayed_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			struct adc_jack_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct adc_jack_cond *def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int ret, adc_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	ret = iio_read_channel_raw(data->chan, &adc_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		dev_err(data->dev, "read channel() error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* Get state from adc value with adc_conditions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	for (i = 0; i < data->num_conditions; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		def = &data->adc_conditions[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			extcon_set_state_sync(data->edev, def->id, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	/* Set the detached state if adc value is not included in the range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	for (i = 0; i < data->num_conditions; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		def = &data->adc_conditions[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		extcon_set_state_sync(data->edev, def->id, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct adc_jack_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	queue_delayed_work(system_power_efficient_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			   &data->handler, data->handling_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static int adc_jack_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct adc_jack_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (!pdata->cable_names) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		dev_err(&pdev->dev, "error: cable_names not defined.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	data->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (IS_ERR(data->edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		dev_err(&pdev->dev, "failed to allocate extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (!pdata->adc_conditions) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	data->adc_conditions = pdata->adc_conditions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/* Check the length of array and set num_conditions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	data->num_conditions = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	data->chan = devm_iio_channel_get(&pdev->dev, pdata->consumer_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (IS_ERR(data->chan))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return PTR_ERR(data->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	data->wakeup_source = pdata->wakeup_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	platform_set_drvdata(pdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	err = devm_extcon_dev_register(&pdev->dev, data->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	data->irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (data->irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	err = request_any_context_irq(data->irq, adc_jack_irq_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			pdata->irq_flags, pdata->name, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		dev_err(&pdev->dev, "error: irq %d\n", data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (data->wakeup_source)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		device_init_wakeup(&pdev->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	adc_jack_handler(&data->handler.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int adc_jack_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct adc_jack_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	free_irq(data->irq, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	cancel_work_sync(&data->handler.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int adc_jack_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct adc_jack_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	cancel_delayed_work_sync(&data->handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (device_may_wakeup(data->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		enable_irq_wake(data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int adc_jack_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct adc_jack_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (device_may_wakeup(data->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		disable_irq_wake(data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) #endif /* CONFIG_PM_SLEEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		adc_jack_suspend, adc_jack_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static struct platform_driver adc_jack_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.probe          = adc_jack_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.remove         = adc_jack_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.driver         = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		.name   = "adc-jack",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		.pm = &adc_jack_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) module_platform_driver(adc_jack_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_DESCRIPTION("ADC Jack extcon driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) MODULE_LICENSE("GPL v2");