Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Arizona haptics driver
  4 *
  5 * Copyright 2012 Wolfson Microelectronics plc
  6 *
  7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 
 
 
 
  8 */
  9
 10#include <linux/module.h>
 11#include <linux/platform_device.h>
 12#include <linux/input.h>
 13#include <linux/slab.h>
 14
 15#include <sound/soc.h>
 16#include <sound/soc-dapm.h>
 17
 18#include <linux/mfd/arizona/core.h>
 19#include <linux/mfd/arizona/pdata.h>
 20#include <linux/mfd/arizona/registers.h>
 21
 22struct arizona_haptics {
 23	struct arizona *arizona;
 24	struct input_dev *input_dev;
 25	struct work_struct work;
 26
 27	struct mutex mutex;
 28	u8 intensity;
 29};
 30
 31static void arizona_haptics_work(struct work_struct *work)
 32{
 33	struct arizona_haptics *haptics = container_of(work,
 34						       struct arizona_haptics,
 35						       work);
 36	struct arizona *arizona = haptics->arizona;
 37	struct snd_soc_component *component =
 38		snd_soc_dapm_to_component(arizona->dapm);
 39	int ret;
 40
 41	if (!haptics->arizona->dapm) {
 42		dev_err(arizona->dev, "No DAPM context\n");
 43		return;
 44	}
 45
 46	if (haptics->intensity) {
 47		ret = regmap_update_bits(arizona->regmap,
 48					 ARIZONA_HAPTICS_PHASE_2_INTENSITY,
 49					 ARIZONA_PHASE2_INTENSITY_MASK,
 50					 haptics->intensity);
 51		if (ret != 0) {
 52			dev_err(arizona->dev, "Failed to set intensity: %d\n",
 53				ret);
 54			return;
 55		}
 56
 57		/* This enable sequence will be a noop if already enabled */
 58		ret = regmap_update_bits(arizona->regmap,
 59					 ARIZONA_HAPTICS_CONTROL_1,
 60					 ARIZONA_HAP_CTRL_MASK,
 61					 1 << ARIZONA_HAP_CTRL_SHIFT);
 62		if (ret != 0) {
 63			dev_err(arizona->dev, "Failed to start haptics: %d\n",
 64				ret);
 65			return;
 66		}
 67
 68		ret = snd_soc_component_enable_pin(component, "HAPTICS");
 69		if (ret != 0) {
 70			dev_err(arizona->dev, "Failed to start HAPTICS: %d\n",
 71				ret);
 72			return;
 73		}
 74
 75		ret = snd_soc_dapm_sync(arizona->dapm);
 76		if (ret != 0) {
 77			dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
 78				ret);
 79			return;
 80		}
 81	} else {
 82		/* This disable sequence will be a noop if already enabled */
 83		ret = snd_soc_component_disable_pin(component, "HAPTICS");
 84		if (ret != 0) {
 85			dev_err(arizona->dev, "Failed to disable HAPTICS: %d\n",
 86				ret);
 87			return;
 88		}
 89
 90		ret = snd_soc_dapm_sync(arizona->dapm);
 91		if (ret != 0) {
 92			dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
 93				ret);
 94			return;
 95		}
 96
 97		ret = regmap_update_bits(arizona->regmap,
 98					 ARIZONA_HAPTICS_CONTROL_1,
 99					 ARIZONA_HAP_CTRL_MASK, 0);
100		if (ret != 0) {
101			dev_err(arizona->dev, "Failed to stop haptics: %d\n",
102				ret);
103			return;
104		}
105	}
106}
107
108static int arizona_haptics_play(struct input_dev *input, void *data,
109				struct ff_effect *effect)
110{
111	struct arizona_haptics *haptics = input_get_drvdata(input);
112	struct arizona *arizona = haptics->arizona;
113
114	if (!arizona->dapm) {
115		dev_err(arizona->dev, "No DAPM context\n");
116		return -EBUSY;
117	}
118
119	if (effect->u.rumble.strong_magnitude) {
120		/* Scale the magnitude into the range the device supports */
121		if (arizona->pdata.hap_act) {
122			haptics->intensity =
123				effect->u.rumble.strong_magnitude >> 9;
124			if (effect->direction < 0x8000)
125				haptics->intensity += 0x7f;
126		} else {
127			haptics->intensity =
128				effect->u.rumble.strong_magnitude >> 8;
129		}
130	} else {
131		haptics->intensity = 0;
132	}
133
134	schedule_work(&haptics->work);
135
136	return 0;
137}
138
139static void arizona_haptics_close(struct input_dev *input)
140{
141	struct arizona_haptics *haptics = input_get_drvdata(input);
142	struct snd_soc_component *component;
143
144	cancel_work_sync(&haptics->work);
145
146	if (haptics->arizona->dapm) {
147		component = snd_soc_dapm_to_component(haptics->arizona->dapm);
148		snd_soc_component_disable_pin(component, "HAPTICS");
149	}
150}
151
152static int arizona_haptics_probe(struct platform_device *pdev)
153{
154	struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
155	struct arizona_haptics *haptics;
156	int ret;
157
158	haptics = devm_kzalloc(&pdev->dev, sizeof(*haptics), GFP_KERNEL);
159	if (!haptics)
160		return -ENOMEM;
161
162	haptics->arizona = arizona;
163
164	ret = regmap_update_bits(arizona->regmap, ARIZONA_HAPTICS_CONTROL_1,
165				 ARIZONA_HAP_ACT, arizona->pdata.hap_act);
166	if (ret != 0) {
167		dev_err(arizona->dev, "Failed to set haptics actuator: %d\n",
168			ret);
169		return ret;
170	}
171
172	INIT_WORK(&haptics->work, arizona_haptics_work);
173
174	haptics->input_dev = devm_input_allocate_device(&pdev->dev);
175	if (!haptics->input_dev) {
176		dev_err(arizona->dev, "Failed to allocate input device\n");
177		return -ENOMEM;
178	}
179
180	input_set_drvdata(haptics->input_dev, haptics);
181
182	haptics->input_dev->name = "arizona:haptics";
183	haptics->input_dev->close = arizona_haptics_close;
184	__set_bit(FF_RUMBLE, haptics->input_dev->ffbit);
185
186	ret = input_ff_create_memless(haptics->input_dev, NULL,
187				      arizona_haptics_play);
188	if (ret < 0) {
189		dev_err(arizona->dev, "input_ff_create_memless() failed: %d\n",
190			ret);
191		return ret;
192	}
193
194	ret = input_register_device(haptics->input_dev);
195	if (ret < 0) {
196		dev_err(arizona->dev, "couldn't register input device: %d\n",
197			ret);
198		return ret;
199	}
 
 
200
201	return 0;
202}
203
204static struct platform_driver arizona_haptics_driver = {
205	.probe		= arizona_haptics_probe,
206	.driver		= {
207		.name	= "arizona-haptics",
208	},
209};
210module_platform_driver(arizona_haptics_driver);
211
212MODULE_ALIAS("platform:arizona-haptics");
213MODULE_DESCRIPTION("Arizona haptics driver");
214MODULE_LICENSE("GPL");
215MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
v4.10.11
 
  1/*
  2 * Arizona haptics driver
  3 *
  4 * Copyright 2012 Wolfson Microelectronics plc
  5 *
  6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License version 2 as
 10 * published by the Free Software Foundation.
 11 */
 12
 13#include <linux/module.h>
 14#include <linux/platform_device.h>
 15#include <linux/input.h>
 16#include <linux/slab.h>
 17
 18#include <sound/soc.h>
 19#include <sound/soc-dapm.h>
 20
 21#include <linux/mfd/arizona/core.h>
 22#include <linux/mfd/arizona/pdata.h>
 23#include <linux/mfd/arizona/registers.h>
 24
 25struct arizona_haptics {
 26	struct arizona *arizona;
 27	struct input_dev *input_dev;
 28	struct work_struct work;
 29
 30	struct mutex mutex;
 31	u8 intensity;
 32};
 33
 34static void arizona_haptics_work(struct work_struct *work)
 35{
 36	struct arizona_haptics *haptics = container_of(work,
 37						       struct arizona_haptics,
 38						       work);
 39	struct arizona *arizona = haptics->arizona;
 40	struct snd_soc_component *component =
 41		snd_soc_dapm_to_component(arizona->dapm);
 42	int ret;
 43
 44	if (!haptics->arizona->dapm) {
 45		dev_err(arizona->dev, "No DAPM context\n");
 46		return;
 47	}
 48
 49	if (haptics->intensity) {
 50		ret = regmap_update_bits(arizona->regmap,
 51					 ARIZONA_HAPTICS_PHASE_2_INTENSITY,
 52					 ARIZONA_PHASE2_INTENSITY_MASK,
 53					 haptics->intensity);
 54		if (ret != 0) {
 55			dev_err(arizona->dev, "Failed to set intensity: %d\n",
 56				ret);
 57			return;
 58		}
 59
 60		/* This enable sequence will be a noop if already enabled */
 61		ret = regmap_update_bits(arizona->regmap,
 62					 ARIZONA_HAPTICS_CONTROL_1,
 63					 ARIZONA_HAP_CTRL_MASK,
 64					 1 << ARIZONA_HAP_CTRL_SHIFT);
 65		if (ret != 0) {
 66			dev_err(arizona->dev, "Failed to start haptics: %d\n",
 67				ret);
 68			return;
 69		}
 70
 71		ret = snd_soc_component_enable_pin(component, "HAPTICS");
 72		if (ret != 0) {
 73			dev_err(arizona->dev, "Failed to start HAPTICS: %d\n",
 74				ret);
 75			return;
 76		}
 77
 78		ret = snd_soc_dapm_sync(arizona->dapm);
 79		if (ret != 0) {
 80			dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
 81				ret);
 82			return;
 83		}
 84	} else {
 85		/* This disable sequence will be a noop if already enabled */
 86		ret = snd_soc_component_disable_pin(component, "HAPTICS");
 87		if (ret != 0) {
 88			dev_err(arizona->dev, "Failed to disable HAPTICS: %d\n",
 89				ret);
 90			return;
 91		}
 92
 93		ret = snd_soc_dapm_sync(arizona->dapm);
 94		if (ret != 0) {
 95			dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
 96				ret);
 97			return;
 98		}
 99
100		ret = regmap_update_bits(arizona->regmap,
101					 ARIZONA_HAPTICS_CONTROL_1,
102					 ARIZONA_HAP_CTRL_MASK, 0);
103		if (ret != 0) {
104			dev_err(arizona->dev, "Failed to stop haptics: %d\n",
105				ret);
106			return;
107		}
108	}
109}
110
111static int arizona_haptics_play(struct input_dev *input, void *data,
112				struct ff_effect *effect)
113{
114	struct arizona_haptics *haptics = input_get_drvdata(input);
115	struct arizona *arizona = haptics->arizona;
116
117	if (!arizona->dapm) {
118		dev_err(arizona->dev, "No DAPM context\n");
119		return -EBUSY;
120	}
121
122	if (effect->u.rumble.strong_magnitude) {
123		/* Scale the magnitude into the range the device supports */
124		if (arizona->pdata.hap_act) {
125			haptics->intensity =
126				effect->u.rumble.strong_magnitude >> 9;
127			if (effect->direction < 0x8000)
128				haptics->intensity += 0x7f;
129		} else {
130			haptics->intensity =
131				effect->u.rumble.strong_magnitude >> 8;
132		}
133	} else {
134		haptics->intensity = 0;
135	}
136
137	schedule_work(&haptics->work);
138
139	return 0;
140}
141
142static void arizona_haptics_close(struct input_dev *input)
143{
144	struct arizona_haptics *haptics = input_get_drvdata(input);
145	struct snd_soc_component *component;
146
147	cancel_work_sync(&haptics->work);
148
149	if (haptics->arizona->dapm) {
150		component = snd_soc_dapm_to_component(haptics->arizona->dapm);
151		snd_soc_component_disable_pin(component, "HAPTICS");
152	}
153}
154
155static int arizona_haptics_probe(struct platform_device *pdev)
156{
157	struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
158	struct arizona_haptics *haptics;
159	int ret;
160
161	haptics = devm_kzalloc(&pdev->dev, sizeof(*haptics), GFP_KERNEL);
162	if (!haptics)
163		return -ENOMEM;
164
165	haptics->arizona = arizona;
166
167	ret = regmap_update_bits(arizona->regmap, ARIZONA_HAPTICS_CONTROL_1,
168				 ARIZONA_HAP_ACT, arizona->pdata.hap_act);
169	if (ret != 0) {
170		dev_err(arizona->dev, "Failed to set haptics actuator: %d\n",
171			ret);
172		return ret;
173	}
174
175	INIT_WORK(&haptics->work, arizona_haptics_work);
176
177	haptics->input_dev = devm_input_allocate_device(&pdev->dev);
178	if (!haptics->input_dev) {
179		dev_err(arizona->dev, "Failed to allocate input device\n");
180		return -ENOMEM;
181	}
182
183	input_set_drvdata(haptics->input_dev, haptics);
184
185	haptics->input_dev->name = "arizona:haptics";
186	haptics->input_dev->close = arizona_haptics_close;
187	__set_bit(FF_RUMBLE, haptics->input_dev->ffbit);
188
189	ret = input_ff_create_memless(haptics->input_dev, NULL,
190				      arizona_haptics_play);
191	if (ret < 0) {
192		dev_err(arizona->dev, "input_ff_create_memless() failed: %d\n",
193			ret);
194		return ret;
195	}
196
197	ret = input_register_device(haptics->input_dev);
198	if (ret < 0) {
199		dev_err(arizona->dev, "couldn't register input device: %d\n",
200			ret);
201		return ret;
202	}
203
204	platform_set_drvdata(pdev, haptics);
205
206	return 0;
207}
208
209static struct platform_driver arizona_haptics_driver = {
210	.probe		= arizona_haptics_probe,
211	.driver		= {
212		.name	= "arizona-haptics",
213	},
214};
215module_platform_driver(arizona_haptics_driver);
216
217MODULE_ALIAS("platform:arizona-haptics");
218MODULE_DESCRIPTION("Arizona haptics driver");
219MODULE_LICENSE("GPL");
220MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");