Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Texas Instruments' TPS65217 and TPS65218 Power Button Input Driver
  4 *
  5 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  6 * Author: Felipe Balbi <balbi@ti.com>
  7 * Author: Marcin Niestroj <m.niestroj@grinn-global.com>
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/init.h>
 11#include <linux/input.h>
 12#include <linux/interrupt.h>
 13#include <linux/kernel.h>
 14#include <linux/mfd/tps65217.h>
 15#include <linux/mfd/tps65218.h>
 16#include <linux/module.h>
 17#include <linux/of.h>
 18#include <linux/platform_device.h>
 19#include <linux/regmap.h>
 20#include <linux/slab.h>
 21
 22struct tps6521x_data {
 23	unsigned int reg_status;
 24	unsigned int pb_mask;
 25	const char *name;
 26};
 27
 28static const struct tps6521x_data tps65217_data = {
 29	.reg_status = TPS65217_REG_STATUS,
 30	.pb_mask = TPS65217_STATUS_PB,
 31	.name = "tps65217_pwrbutton",
 32};
 33
 34static const struct tps6521x_data tps65218_data = {
 35	.reg_status = TPS65218_REG_STATUS,
 36	.pb_mask = TPS65218_STATUS_PB_STATE,
 37	.name = "tps65218_pwrbutton",
 38};
 39
 40struct tps6521x_pwrbutton {
 41	struct device *dev;
 42	struct regmap *regmap;
 43	struct input_dev *idev;
 44	const struct tps6521x_data *data;
 45	char phys[32];
 46};
 47
 48static const struct of_device_id of_tps6521x_pb_match[] = {
 49	{ .compatible = "ti,tps65217-pwrbutton", .data = &tps65217_data },
 50	{ .compatible = "ti,tps65218-pwrbutton", .data = &tps65218_data },
 51	{ },
 52};
 53MODULE_DEVICE_TABLE(of, of_tps6521x_pb_match);
 54
 55static irqreturn_t tps6521x_pb_irq(int irq, void *_pwr)
 56{
 57	struct tps6521x_pwrbutton *pwr = _pwr;
 58	const struct tps6521x_data *tps_data = pwr->data;
 59	unsigned int reg;
 60	int error;
 61
 62	error = regmap_read(pwr->regmap, tps_data->reg_status, &reg);
 63	if (error) {
 64		dev_err(pwr->dev, "can't read register: %d\n", error);
 65		goto out;
 66	}
 67
 68	if (reg & tps_data->pb_mask) {
 69		input_report_key(pwr->idev, KEY_POWER, 1);
 70		pm_wakeup_event(pwr->dev, 0);
 71	} else {
 72		input_report_key(pwr->idev, KEY_POWER, 0);
 73	}
 74
 75	input_sync(pwr->idev);
 76
 77out:
 78	return IRQ_HANDLED;
 79}
 80
 81static int tps6521x_pb_probe(struct platform_device *pdev)
 82{
 
 83	struct device *dev = &pdev->dev;
 84	struct tps6521x_pwrbutton *pwr;
 85	struct input_dev *idev;
 86	const struct of_device_id *match;
 87	int error;
 88	int irq;
 89
 90	match = of_match_node(of_tps6521x_pb_match, dev->of_node);
 91	if (!match)
 92		return -ENXIO;
 93
 94	pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
 95	if (!pwr)
 96		return -ENOMEM;
 97
 98	pwr->data = match->data;
 99
100	idev = devm_input_allocate_device(dev);
101	if (!idev)
102		return -ENOMEM;
103
104	idev->name = pwr->data->name;
105	snprintf(pwr->phys, sizeof(pwr->phys), "%s/input0",
106		pwr->data->name);
107	idev->phys = pwr->phys;
108	idev->dev.parent = dev;
109	idev->id.bustype = BUS_I2C;
110
111	input_set_capability(idev, EV_KEY, KEY_POWER);
112
113	pwr->regmap = dev_get_regmap(dev->parent, NULL);
114	pwr->dev = dev;
115	pwr->idev = idev;
 
116	device_init_wakeup(dev, true);
117
118	irq = platform_get_irq(pdev, 0);
119	if (irq < 0)
120		return -EINVAL;
121
122	error = devm_request_threaded_irq(dev, irq, NULL, tps6521x_pb_irq,
123					  IRQF_TRIGGER_RISING |
124						IRQF_TRIGGER_FALLING |
125						IRQF_ONESHOT,
126					  pwr->data->name, pwr);
127	if (error) {
128		dev_err(dev, "failed to request IRQ #%d: %d\n", irq, error);
 
129		return error;
130	}
131
132	error= input_register_device(idev);
133	if (error) {
134		dev_err(dev, "Can't register power button: %d\n", error);
135		return error;
136	}
137
138	return 0;
139}
140
141static const struct platform_device_id tps6521x_pwrbtn_id_table[] = {
142	{ "tps65218-pwrbutton", },
143	{ "tps65217-pwrbutton", },
144	{ /* sentinel */ }
145};
146MODULE_DEVICE_TABLE(platform, tps6521x_pwrbtn_id_table);
147
148static struct platform_driver tps6521x_pb_driver = {
149	.probe	= tps6521x_pb_probe,
150	.driver	= {
151		.name	= "tps6521x_pwrbutton",
152		.of_match_table = of_tps6521x_pb_match,
153	},
154	.id_table = tps6521x_pwrbtn_id_table,
155};
156module_platform_driver(tps6521x_pb_driver);
157
158MODULE_DESCRIPTION("TPS6521X Power Button");
159MODULE_LICENSE("GPL v2");
160MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
v4.6
 
  1/*
  2 * Texas Instruments' TPS65218 Power Button Input Driver
  3 *
  4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  5 * Author: Felipe Balbi <balbi@ti.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License version 2 as
  9 * published by the Free Software Foundation.
 10 *
 11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 12 * kind, whether express or implied; without even the implied warranty
 13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 */
 16
 17#include <linux/init.h>
 18#include <linux/input.h>
 19#include <linux/interrupt.h>
 20#include <linux/kernel.h>
 
 21#include <linux/mfd/tps65218.h>
 22#include <linux/module.h>
 23#include <linux/of.h>
 24#include <linux/platform_device.h>
 
 25#include <linux/slab.h>
 26
 27struct tps65218_pwrbutton {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 28	struct device *dev;
 29	struct tps65218 *tps;
 30	struct input_dev *idev;
 
 
 31};
 32
 33static irqreturn_t tps65218_pwr_irq(int irq, void *_pwr)
 
 
 
 
 
 
 
 34{
 35	struct tps65218_pwrbutton *pwr = _pwr;
 
 36	unsigned int reg;
 37	int error;
 38
 39	error = tps65218_reg_read(pwr->tps, TPS65218_REG_STATUS, &reg);
 40	if (error) {
 41		dev_err(pwr->dev, "can't read register: %d\n", error);
 42		goto out;
 43	}
 44
 45	if (reg & TPS65218_STATUS_PB_STATE) {
 46		input_report_key(pwr->idev, KEY_POWER, 1);
 47		pm_wakeup_event(pwr->dev, 0);
 48	} else {
 49		input_report_key(pwr->idev, KEY_POWER, 0);
 50	}
 51
 52	input_sync(pwr->idev);
 53
 54out:
 55	return IRQ_HANDLED;
 56}
 57
 58static int tps65218_pwron_probe(struct platform_device *pdev)
 59{
 60	struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
 61	struct device *dev = &pdev->dev;
 62	struct tps65218_pwrbutton *pwr;
 63	struct input_dev *idev;
 
 64	int error;
 65	int irq;
 66
 
 
 
 
 67	pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
 68	if (!pwr)
 69		return -ENOMEM;
 70
 
 
 71	idev = devm_input_allocate_device(dev);
 72	if (!idev)
 73		return -ENOMEM;
 74
 75	idev->name = "tps65218_pwrbutton";
 76	idev->phys = "tps65218_pwrbutton/input0";
 
 
 77	idev->dev.parent = dev;
 78	idev->id.bustype = BUS_I2C;
 79
 80	input_set_capability(idev, EV_KEY, KEY_POWER);
 81
 82	pwr->tps = tps;
 83	pwr->dev = dev;
 84	pwr->idev = idev;
 85	platform_set_drvdata(pdev, pwr);
 86	device_init_wakeup(dev, true);
 87
 88	irq = platform_get_irq(pdev, 0);
 89	error = devm_request_threaded_irq(dev, irq, NULL, tps65218_pwr_irq,
 
 
 
 90					  IRQF_TRIGGER_RISING |
 91						IRQF_TRIGGER_FALLING |
 92						IRQF_ONESHOT,
 93					  "tps65218-pwrbutton", pwr);
 94	if (error) {
 95		dev_err(dev, "failed to request IRQ #%d: %d\n",
 96			irq, error);
 97		return error;
 98	}
 99
100	error= input_register_device(idev);
101	if (error) {
102		dev_err(dev, "Can't register power button: %d\n", error);
103		return error;
104	}
105
106	return 0;
107}
108
109static const struct of_device_id of_tps65218_pwr_match[] = {
110	{ .compatible = "ti,tps65218-pwrbutton" },
111	{ },
 
112};
113MODULE_DEVICE_TABLE(of, of_tps65218_pwr_match);
114
115static struct platform_driver tps65218_pwron_driver = {
116	.probe	= tps65218_pwron_probe,
117	.driver	= {
118		.name	= "tps65218_pwrbutton",
119		.of_match_table = of_tps65218_pwr_match,
120	},
 
121};
122module_platform_driver(tps65218_pwron_driver);
123
124MODULE_DESCRIPTION("TPS65218 Power Button");
125MODULE_LICENSE("GPL v2");
126MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");