Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Apr 14-17, 2025
Register
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * DRV2667 haptics driver family
  4 *
  5 * Author: Dan Murphy <dmurphy@ti.com>
  6 *
  7 * Copyright: (C) 2014 Texas Instruments, Inc.
  8 */
  9
 10#include <linux/i2c.h>
 11#include <linux/input.h>
 12#include <linux/module.h>
 13#include <linux/platform_device.h>
 14#include <linux/regmap.h>
 15#include <linux/slab.h>
 16#include <linux/delay.h>
 17#include <linux/regulator/consumer.h>
 18
 19/* Contol registers */
 20#define DRV2667_STATUS	0x00
 21#define DRV2667_CTRL_1	0x01
 22#define DRV2667_CTRL_2	0x02
 23/* Waveform sequencer */
 24#define DRV2667_WV_SEQ_0	0x03
 25#define DRV2667_WV_SEQ_1	0x04
 26#define DRV2667_WV_SEQ_2	0x05
 27#define DRV2667_WV_SEQ_3	0x06
 28#define DRV2667_WV_SEQ_4	0x07
 29#define DRV2667_WV_SEQ_5	0x08
 30#define DRV2667_WV_SEQ_6	0x09
 31#define DRV2667_WV_SEQ_7	0x0A
 32#define DRV2667_FIFO		0x0B
 33#define DRV2667_PAGE		0xFF
 34#define DRV2667_MAX_REG		DRV2667_PAGE
 35
 36#define DRV2667_PAGE_0		0x00
 37#define DRV2667_PAGE_1		0x01
 38#define DRV2667_PAGE_2		0x02
 39#define DRV2667_PAGE_3		0x03
 40#define DRV2667_PAGE_4		0x04
 41#define DRV2667_PAGE_5		0x05
 42#define DRV2667_PAGE_6		0x06
 43#define DRV2667_PAGE_7		0x07
 44#define DRV2667_PAGE_8		0x08
 45
 46/* RAM fields */
 47#define DRV2667_RAM_HDR_SZ	0x0
 48/* RAM Header addresses */
 49#define DRV2667_RAM_START_HI	0x01
 50#define DRV2667_RAM_START_LO	0x02
 51#define DRV2667_RAM_STOP_HI		0x03
 52#define DRV2667_RAM_STOP_LO		0x04
 53#define DRV2667_RAM_REPEAT_CT	0x05
 54/* RAM data addresses */
 55#define DRV2667_RAM_AMP		0x06
 56#define DRV2667_RAM_FREQ	0x07
 57#define DRV2667_RAM_DURATION	0x08
 58#define DRV2667_RAM_ENVELOPE	0x09
 59
 60/* Control 1 Register */
 61#define DRV2667_25_VPP_GAIN		0x00
 62#define DRV2667_50_VPP_GAIN		0x01
 63#define DRV2667_75_VPP_GAIN		0x02
 64#define DRV2667_100_VPP_GAIN	0x03
 65#define DRV2667_DIGITAL_IN		0xfc
 66#define DRV2667_ANALOG_IN		(1 << 2)
 67
 68/* Control 2 Register */
 69#define DRV2667_GO			(1 << 0)
 70#define DRV2667_STANDBY		(1 << 6)
 71#define DRV2667_DEV_RST		(1 << 7)
 72
 73/* RAM Envelope settings */
 74#define DRV2667_NO_ENV			0x00
 75#define DRV2667_32_MS_ENV		0x01
 76#define DRV2667_64_MS_ENV		0x02
 77#define DRV2667_96_MS_ENV		0x03
 78#define DRV2667_128_MS_ENV		0x04
 79#define DRV2667_160_MS_ENV		0x05
 80#define DRV2667_192_MS_ENV		0x06
 81#define DRV2667_224_MS_ENV		0x07
 82#define DRV2667_256_MS_ENV		0x08
 83#define DRV2667_512_MS_ENV		0x09
 84#define DRV2667_768_MS_ENV		0x0a
 85#define DRV2667_1024_MS_ENV		0x0b
 86#define DRV2667_1280_MS_ENV		0x0c
 87#define DRV2667_1536_MS_ENV		0x0d
 88#define DRV2667_1792_MS_ENV		0x0e
 89#define DRV2667_2048_MS_ENV		0x0f
 90
 91/**
 92 * struct drv2667_data -
 93 * @input_dev: Pointer to the input device
 94 * @client: Pointer to the I2C client
 95 * @regmap: Register map of the device
 96 * @work: Work item used to off load the enable/disable of the vibration
 97 * @regulator: Pointer to the regulator for the IC
 98 * @page: Page number
 99 * @magnitude: Magnitude of the vibration event
100 * @frequency: Frequency of the vibration event
101**/
102struct drv2667_data {
103	struct input_dev *input_dev;
104	struct i2c_client *client;
105	struct regmap *regmap;
106	struct work_struct work;
107	struct regulator *regulator;
108	u32 page;
109	u32 magnitude;
110	u32 frequency;
111};
112
113static const struct reg_default drv2667_reg_defs[] = {
114	{ DRV2667_STATUS, 0x02 },
115	{ DRV2667_CTRL_1, 0x28 },
116	{ DRV2667_CTRL_2, 0x40 },
117	{ DRV2667_WV_SEQ_0, 0x00 },
118	{ DRV2667_WV_SEQ_1, 0x00 },
119	{ DRV2667_WV_SEQ_2, 0x00 },
120	{ DRV2667_WV_SEQ_3, 0x00 },
121	{ DRV2667_WV_SEQ_4, 0x00 },
122	{ DRV2667_WV_SEQ_5, 0x00 },
123	{ DRV2667_WV_SEQ_6, 0x00 },
124	{ DRV2667_WV_SEQ_7, 0x00 },
125	{ DRV2667_FIFO, 0x00 },
126	{ DRV2667_PAGE, 0x00 },
127};
128
129static int drv2667_set_waveform_freq(struct drv2667_data *haptics)
130{
131	unsigned int read_buf;
132	int freq;
133	int error;
134
135	/* Per the data sheet:
136	 * Sinusoid Frequency (Hz) = 7.8125 x Frequency
137	 */
138	freq = (haptics->frequency * 1000) / 78125;
139	if (freq <= 0) {
140		dev_err(&haptics->client->dev,
141			"ERROR: Frequency calculated to %i\n", freq);
142		return -EINVAL;
143	}
144
145	error = regmap_read(haptics->regmap, DRV2667_PAGE, &read_buf);
146	if (error) {
147		dev_err(&haptics->client->dev,
148			"Failed to read the page number: %d\n", error);
149		return -EIO;
150	}
151
152	if (read_buf == DRV2667_PAGE_0 ||
153		haptics->page != read_buf) {
154		error = regmap_write(haptics->regmap,
155				DRV2667_PAGE, haptics->page);
156		if (error) {
157			dev_err(&haptics->client->dev,
158				"Failed to set the page: %d\n", error);
159			return -EIO;
160		}
161	}
162
163	error = regmap_write(haptics->regmap, DRV2667_RAM_FREQ,	freq);
164	if (error)
165		dev_err(&haptics->client->dev,
166				"Failed to set the frequency: %d\n", error);
167
168	/* Reset back to original page */
169	if (read_buf == DRV2667_PAGE_0 ||
170		haptics->page != read_buf) {
171		error = regmap_write(haptics->regmap, DRV2667_PAGE, read_buf);
172		if (error) {
173			dev_err(&haptics->client->dev,
174				"Failed to set the page: %d\n", error);
175			return -EIO;
176		}
177	}
178
179	return error;
180}
181
182static void drv2667_worker(struct work_struct *work)
183{
184	struct drv2667_data *haptics = container_of(work, struct drv2667_data, work);
185	int error;
186
187	if (haptics->magnitude) {
188		error = regmap_write(haptics->regmap,
189				DRV2667_PAGE, haptics->page);
190		if (error) {
191			dev_err(&haptics->client->dev,
192				"Failed to set the page: %d\n", error);
193			return;
194		}
195
196		error = regmap_write(haptics->regmap, DRV2667_RAM_AMP,
197				haptics->magnitude);
198		if (error) {
199			dev_err(&haptics->client->dev,
200				"Failed to set the amplitude: %d\n", error);
201			return;
202		}
203
204		error = regmap_write(haptics->regmap,
205				DRV2667_PAGE, DRV2667_PAGE_0);
206		if (error) {
207			dev_err(&haptics->client->dev,
208				"Failed to set the page: %d\n", error);
209			return;
210		}
211
212		error = regmap_write(haptics->regmap,
213				DRV2667_CTRL_2, DRV2667_GO);
214		if (error) {
215			dev_err(&haptics->client->dev,
216				"Failed to set the GO bit: %d\n", error);
217		}
218	} else {
219		error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
220				DRV2667_GO, 0);
221		if (error) {
222			dev_err(&haptics->client->dev,
223				"Failed to unset the GO bit: %d\n", error);
224		}
225	}
226}
227
228static int drv2667_haptics_play(struct input_dev *input, void *data,
229				struct ff_effect *effect)
230{
231	struct drv2667_data *haptics = input_get_drvdata(input);
232
233	if (effect->u.rumble.strong_magnitude > 0)
234		haptics->magnitude = effect->u.rumble.strong_magnitude;
235	else if (effect->u.rumble.weak_magnitude > 0)
236		haptics->magnitude = effect->u.rumble.weak_magnitude;
237	else
238		haptics->magnitude = 0;
239
240	schedule_work(&haptics->work);
241
242	return 0;
243}
244
245static void drv2667_close(struct input_dev *input)
246{
247	struct drv2667_data *haptics = input_get_drvdata(input);
248	int error;
249
250	cancel_work_sync(&haptics->work);
251
252	error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
253				   DRV2667_STANDBY, DRV2667_STANDBY);
254	if (error)
255		dev_err(&haptics->client->dev,
256			"Failed to enter standby mode: %d\n", error);
257}
258
259static const struct reg_sequence drv2667_init_regs[] = {
260	{ DRV2667_CTRL_2, 0 },
261	{ DRV2667_CTRL_1, DRV2667_25_VPP_GAIN },
262	{ DRV2667_WV_SEQ_0, 1 },
263	{ DRV2667_WV_SEQ_1, 0 }
264};
265
266static const struct reg_sequence drv2667_page1_init[] = {
267	{ DRV2667_RAM_HDR_SZ, 0x05 },
268	{ DRV2667_RAM_START_HI, 0x80 },
269	{ DRV2667_RAM_START_LO, 0x06 },
270	{ DRV2667_RAM_STOP_HI, 0x00 },
271	{ DRV2667_RAM_STOP_LO, 0x09 },
272	{ DRV2667_RAM_REPEAT_CT, 0 },
273	{ DRV2667_RAM_DURATION, 0x05 },
274	{ DRV2667_RAM_ENVELOPE, DRV2667_NO_ENV },
275	{ DRV2667_RAM_AMP, 0x60 },
276};
277
278static int drv2667_init(struct drv2667_data *haptics)
279{
280	int error;
281
282	/* Set default haptic frequency to 195Hz on Page 1*/
283	haptics->frequency = 195;
284	haptics->page = DRV2667_PAGE_1;
285
286	error = regmap_register_patch(haptics->regmap,
287				      drv2667_init_regs,
288				      ARRAY_SIZE(drv2667_init_regs));
289	if (error) {
290		dev_err(&haptics->client->dev,
291			"Failed to write init registers: %d\n",
292			error);
293		return error;
294	}
295
296	error = regmap_write(haptics->regmap, DRV2667_PAGE, haptics->page);
297	if (error) {
298		dev_err(&haptics->client->dev, "Failed to set page: %d\n",
299			error);
300		goto error_out;
301	}
302
303	error = drv2667_set_waveform_freq(haptics);
304	if (error)
305		goto error_page;
306
307	error = regmap_register_patch(haptics->regmap,
308				      drv2667_page1_init,
309				      ARRAY_SIZE(drv2667_page1_init));
310	if (error) {
311		dev_err(&haptics->client->dev,
312			"Failed to write page registers: %d\n",
313			error);
314		return error;
315	}
316
317	error = regmap_write(haptics->regmap, DRV2667_PAGE, DRV2667_PAGE_0);
318	return error;
319
320error_page:
321	regmap_write(haptics->regmap, DRV2667_PAGE, DRV2667_PAGE_0);
322error_out:
323	return error;
324}
325
326static const struct regmap_config drv2667_regmap_config = {
327	.reg_bits = 8,
328	.val_bits = 8,
329
330	.max_register = DRV2667_MAX_REG,
331	.reg_defaults = drv2667_reg_defs,
332	.num_reg_defaults = ARRAY_SIZE(drv2667_reg_defs),
333	.cache_type = REGCACHE_NONE,
334};
335
336static int drv2667_probe(struct i2c_client *client)
 
337{
338	struct drv2667_data *haptics;
339	int error;
340
341	haptics = devm_kzalloc(&client->dev, sizeof(*haptics), GFP_KERNEL);
342	if (!haptics)
343		return -ENOMEM;
344
345	haptics->regulator = devm_regulator_get(&client->dev, "vbat");
346	if (IS_ERR(haptics->regulator)) {
347		error = PTR_ERR(haptics->regulator);
348		dev_err(&client->dev,
349			"unable to get regulator, error: %d\n", error);
350		return error;
351	}
352
353	haptics->input_dev = devm_input_allocate_device(&client->dev);
354	if (!haptics->input_dev) {
355		dev_err(&client->dev, "Failed to allocate input device\n");
356		return -ENOMEM;
357	}
358
359	haptics->input_dev->name = "drv2667:haptics";
360	haptics->input_dev->dev.parent = client->dev.parent;
361	haptics->input_dev->close = drv2667_close;
362	input_set_drvdata(haptics->input_dev, haptics);
363	input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
364
365	error = input_ff_create_memless(haptics->input_dev, NULL,
366					drv2667_haptics_play);
367	if (error) {
368		dev_err(&client->dev, "input_ff_create() failed: %d\n",
369			error);
370		return error;
371	}
372
373	INIT_WORK(&haptics->work, drv2667_worker);
374
375	haptics->client = client;
376	i2c_set_clientdata(client, haptics);
377
378	haptics->regmap = devm_regmap_init_i2c(client, &drv2667_regmap_config);
379	if (IS_ERR(haptics->regmap)) {
380		error = PTR_ERR(haptics->regmap);
381		dev_err(&client->dev, "Failed to allocate register map: %d\n",
382			error);
383		return error;
384	}
385
386	error = drv2667_init(haptics);
387	if (error) {
388		dev_err(&client->dev, "Device init failed: %d\n", error);
389		return error;
390	}
391
392	error = input_register_device(haptics->input_dev);
393	if (error) {
394		dev_err(&client->dev, "couldn't register input device: %d\n",
395			error);
396		return error;
397	}
398
399	return 0;
400}
401
402static int __maybe_unused drv2667_suspend(struct device *dev)
403{
404	struct drv2667_data *haptics = dev_get_drvdata(dev);
405	int ret = 0;
406
407	mutex_lock(&haptics->input_dev->mutex);
408
409	if (input_device_enabled(haptics->input_dev)) {
410		ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
411					 DRV2667_STANDBY, DRV2667_STANDBY);
412		if (ret) {
413			dev_err(dev, "Failed to set standby mode\n");
414			regulator_disable(haptics->regulator);
415			goto out;
416		}
417
418		ret = regulator_disable(haptics->regulator);
419		if (ret) {
420			dev_err(dev, "Failed to disable regulator\n");
421			regmap_update_bits(haptics->regmap,
422					   DRV2667_CTRL_2,
423					   DRV2667_STANDBY, 0);
424		}
425	}
426out:
427	mutex_unlock(&haptics->input_dev->mutex);
428	return ret;
429}
430
431static int __maybe_unused drv2667_resume(struct device *dev)
432{
433	struct drv2667_data *haptics = dev_get_drvdata(dev);
434	int ret = 0;
435
436	mutex_lock(&haptics->input_dev->mutex);
437
438	if (input_device_enabled(haptics->input_dev)) {
439		ret = regulator_enable(haptics->regulator);
440		if (ret) {
441			dev_err(dev, "Failed to enable regulator\n");
442			goto out;
443		}
444
445		ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
446					 DRV2667_STANDBY, 0);
447		if (ret) {
448			dev_err(dev, "Failed to unset standby mode\n");
449			regulator_disable(haptics->regulator);
450			goto out;
451		}
452
453	}
454
455out:
456	mutex_unlock(&haptics->input_dev->mutex);
457	return ret;
458}
459
460static SIMPLE_DEV_PM_OPS(drv2667_pm_ops, drv2667_suspend, drv2667_resume);
461
462static const struct i2c_device_id drv2667_id[] = {
463	{ "drv2667", 0 },
464	{ }
465};
466MODULE_DEVICE_TABLE(i2c, drv2667_id);
467
468#ifdef CONFIG_OF
469static const struct of_device_id drv2667_of_match[] = {
470	{ .compatible = "ti,drv2667", },
471	{ }
472};
473MODULE_DEVICE_TABLE(of, drv2667_of_match);
474#endif
475
476static struct i2c_driver drv2667_driver = {
477	.probe_new	= drv2667_probe,
478	.driver		= {
479		.name	= "drv2667-haptics",
480		.of_match_table = of_match_ptr(drv2667_of_match),
481		.pm	= &drv2667_pm_ops,
482	},
483	.id_table = drv2667_id,
484};
485module_i2c_driver(drv2667_driver);
486
487MODULE_DESCRIPTION("TI DRV2667 haptics driver");
488MODULE_LICENSE("GPL");
489MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * DRV2667 haptics driver family
  4 *
  5 * Author: Dan Murphy <dmurphy@ti.com>
  6 *
  7 * Copyright: (C) 2014 Texas Instruments, Inc.
  8 */
  9
 10#include <linux/i2c.h>
 11#include <linux/input.h>
 12#include <linux/module.h>
 13#include <linux/platform_device.h>
 14#include <linux/regmap.h>
 15#include <linux/slab.h>
 16#include <linux/delay.h>
 17#include <linux/regulator/consumer.h>
 18
 19/* Contol registers */
 20#define DRV2667_STATUS	0x00
 21#define DRV2667_CTRL_1	0x01
 22#define DRV2667_CTRL_2	0x02
 23/* Waveform sequencer */
 24#define DRV2667_WV_SEQ_0	0x03
 25#define DRV2667_WV_SEQ_1	0x04
 26#define DRV2667_WV_SEQ_2	0x05
 27#define DRV2667_WV_SEQ_3	0x06
 28#define DRV2667_WV_SEQ_4	0x07
 29#define DRV2667_WV_SEQ_5	0x08
 30#define DRV2667_WV_SEQ_6	0x09
 31#define DRV2667_WV_SEQ_7	0x0A
 32#define DRV2667_FIFO		0x0B
 33#define DRV2667_PAGE		0xFF
 34#define DRV2667_MAX_REG		DRV2667_PAGE
 35
 36#define DRV2667_PAGE_0		0x00
 37#define DRV2667_PAGE_1		0x01
 38#define DRV2667_PAGE_2		0x02
 39#define DRV2667_PAGE_3		0x03
 40#define DRV2667_PAGE_4		0x04
 41#define DRV2667_PAGE_5		0x05
 42#define DRV2667_PAGE_6		0x06
 43#define DRV2667_PAGE_7		0x07
 44#define DRV2667_PAGE_8		0x08
 45
 46/* RAM fields */
 47#define DRV2667_RAM_HDR_SZ	0x0
 48/* RAM Header addresses */
 49#define DRV2667_RAM_START_HI	0x01
 50#define DRV2667_RAM_START_LO	0x02
 51#define DRV2667_RAM_STOP_HI		0x03
 52#define DRV2667_RAM_STOP_LO		0x04
 53#define DRV2667_RAM_REPEAT_CT	0x05
 54/* RAM data addresses */
 55#define DRV2667_RAM_AMP		0x06
 56#define DRV2667_RAM_FREQ	0x07
 57#define DRV2667_RAM_DURATION	0x08
 58#define DRV2667_RAM_ENVELOPE	0x09
 59
 60/* Control 1 Register */
 61#define DRV2667_25_VPP_GAIN		0x00
 62#define DRV2667_50_VPP_GAIN		0x01
 63#define DRV2667_75_VPP_GAIN		0x02
 64#define DRV2667_100_VPP_GAIN	0x03
 65#define DRV2667_DIGITAL_IN		0xfc
 66#define DRV2667_ANALOG_IN		(1 << 2)
 67
 68/* Control 2 Register */
 69#define DRV2667_GO			(1 << 0)
 70#define DRV2667_STANDBY		(1 << 6)
 71#define DRV2667_DEV_RST		(1 << 7)
 72
 73/* RAM Envelope settings */
 74#define DRV2667_NO_ENV			0x00
 75#define DRV2667_32_MS_ENV		0x01
 76#define DRV2667_64_MS_ENV		0x02
 77#define DRV2667_96_MS_ENV		0x03
 78#define DRV2667_128_MS_ENV		0x04
 79#define DRV2667_160_MS_ENV		0x05
 80#define DRV2667_192_MS_ENV		0x06
 81#define DRV2667_224_MS_ENV		0x07
 82#define DRV2667_256_MS_ENV		0x08
 83#define DRV2667_512_MS_ENV		0x09
 84#define DRV2667_768_MS_ENV		0x0a
 85#define DRV2667_1024_MS_ENV		0x0b
 86#define DRV2667_1280_MS_ENV		0x0c
 87#define DRV2667_1536_MS_ENV		0x0d
 88#define DRV2667_1792_MS_ENV		0x0e
 89#define DRV2667_2048_MS_ENV		0x0f
 90
 91/**
 92 * struct drv2667_data -
 93 * @input_dev - Pointer to the input device
 94 * @client - Pointer to the I2C client
 95 * @regmap - Register map of the device
 96 * @work - Work item used to off load the enable/disable of the vibration
 97 * @regulator - Pointer to the regulator for the IC
 98 * @magnitude - Magnitude of the vibration event
 
 
 99**/
100struct drv2667_data {
101	struct input_dev *input_dev;
102	struct i2c_client *client;
103	struct regmap *regmap;
104	struct work_struct work;
105	struct regulator *regulator;
106	u32 page;
107	u32 magnitude;
108	u32 frequency;
109};
110
111static const struct reg_default drv2667_reg_defs[] = {
112	{ DRV2667_STATUS, 0x02 },
113	{ DRV2667_CTRL_1, 0x28 },
114	{ DRV2667_CTRL_2, 0x40 },
115	{ DRV2667_WV_SEQ_0, 0x00 },
116	{ DRV2667_WV_SEQ_1, 0x00 },
117	{ DRV2667_WV_SEQ_2, 0x00 },
118	{ DRV2667_WV_SEQ_3, 0x00 },
119	{ DRV2667_WV_SEQ_4, 0x00 },
120	{ DRV2667_WV_SEQ_5, 0x00 },
121	{ DRV2667_WV_SEQ_6, 0x00 },
122	{ DRV2667_WV_SEQ_7, 0x00 },
123	{ DRV2667_FIFO, 0x00 },
124	{ DRV2667_PAGE, 0x00 },
125};
126
127static int drv2667_set_waveform_freq(struct drv2667_data *haptics)
128{
129	unsigned int read_buf;
130	int freq;
131	int error;
132
133	/* Per the data sheet:
134	 * Sinusoid Frequency (Hz) = 7.8125 x Frequency
135	 */
136	freq = (haptics->frequency * 1000) / 78125;
137	if (freq <= 0) {
138		dev_err(&haptics->client->dev,
139			"ERROR: Frequency calculated to %i\n", freq);
140		return -EINVAL;
141	}
142
143	error = regmap_read(haptics->regmap, DRV2667_PAGE, &read_buf);
144	if (error) {
145		dev_err(&haptics->client->dev,
146			"Failed to read the page number: %d\n", error);
147		return -EIO;
148	}
149
150	if (read_buf == DRV2667_PAGE_0 ||
151		haptics->page != read_buf) {
152		error = regmap_write(haptics->regmap,
153				DRV2667_PAGE, haptics->page);
154		if (error) {
155			dev_err(&haptics->client->dev,
156				"Failed to set the page: %d\n", error);
157			return -EIO;
158		}
159	}
160
161	error = regmap_write(haptics->regmap, DRV2667_RAM_FREQ,	freq);
162	if (error)
163		dev_err(&haptics->client->dev,
164				"Failed to set the frequency: %d\n", error);
165
166	/* Reset back to original page */
167	if (read_buf == DRV2667_PAGE_0 ||
168		haptics->page != read_buf) {
169		error = regmap_write(haptics->regmap, DRV2667_PAGE, read_buf);
170		if (error) {
171			dev_err(&haptics->client->dev,
172				"Failed to set the page: %d\n", error);
173			return -EIO;
174		}
175	}
176
177	return error;
178}
179
180static void drv2667_worker(struct work_struct *work)
181{
182	struct drv2667_data *haptics = container_of(work, struct drv2667_data, work);
183	int error;
184
185	if (haptics->magnitude) {
186		error = regmap_write(haptics->regmap,
187				DRV2667_PAGE, haptics->page);
188		if (error) {
189			dev_err(&haptics->client->dev,
190				"Failed to set the page: %d\n", error);
191			return;
192		}
193
194		error = regmap_write(haptics->regmap, DRV2667_RAM_AMP,
195				haptics->magnitude);
196		if (error) {
197			dev_err(&haptics->client->dev,
198				"Failed to set the amplitude: %d\n", error);
199			return;
200		}
201
202		error = regmap_write(haptics->regmap,
203				DRV2667_PAGE, DRV2667_PAGE_0);
204		if (error) {
205			dev_err(&haptics->client->dev,
206				"Failed to set the page: %d\n", error);
207			return;
208		}
209
210		error = regmap_write(haptics->regmap,
211				DRV2667_CTRL_2, DRV2667_GO);
212		if (error) {
213			dev_err(&haptics->client->dev,
214				"Failed to set the GO bit: %d\n", error);
215		}
216	} else {
217		error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
218				DRV2667_GO, 0);
219		if (error) {
220			dev_err(&haptics->client->dev,
221				"Failed to unset the GO bit: %d\n", error);
222		}
223	}
224}
225
226static int drv2667_haptics_play(struct input_dev *input, void *data,
227				struct ff_effect *effect)
228{
229	struct drv2667_data *haptics = input_get_drvdata(input);
230
231	if (effect->u.rumble.strong_magnitude > 0)
232		haptics->magnitude = effect->u.rumble.strong_magnitude;
233	else if (effect->u.rumble.weak_magnitude > 0)
234		haptics->magnitude = effect->u.rumble.weak_magnitude;
235	else
236		haptics->magnitude = 0;
237
238	schedule_work(&haptics->work);
239
240	return 0;
241}
242
243static void drv2667_close(struct input_dev *input)
244{
245	struct drv2667_data *haptics = input_get_drvdata(input);
246	int error;
247
248	cancel_work_sync(&haptics->work);
249
250	error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
251				   DRV2667_STANDBY, DRV2667_STANDBY);
252	if (error)
253		dev_err(&haptics->client->dev,
254			"Failed to enter standby mode: %d\n", error);
255}
256
257static const struct reg_sequence drv2667_init_regs[] = {
258	{ DRV2667_CTRL_2, 0 },
259	{ DRV2667_CTRL_1, DRV2667_25_VPP_GAIN },
260	{ DRV2667_WV_SEQ_0, 1 },
261	{ DRV2667_WV_SEQ_1, 0 }
262};
263
264static const struct reg_sequence drv2667_page1_init[] = {
265	{ DRV2667_RAM_HDR_SZ, 0x05 },
266	{ DRV2667_RAM_START_HI, 0x80 },
267	{ DRV2667_RAM_START_LO, 0x06 },
268	{ DRV2667_RAM_STOP_HI, 0x00 },
269	{ DRV2667_RAM_STOP_LO, 0x09 },
270	{ DRV2667_RAM_REPEAT_CT, 0 },
271	{ DRV2667_RAM_DURATION, 0x05 },
272	{ DRV2667_RAM_ENVELOPE, DRV2667_NO_ENV },
273	{ DRV2667_RAM_AMP, 0x60 },
274};
275
276static int drv2667_init(struct drv2667_data *haptics)
277{
278	int error;
279
280	/* Set default haptic frequency to 195Hz on Page 1*/
281	haptics->frequency = 195;
282	haptics->page = DRV2667_PAGE_1;
283
284	error = regmap_register_patch(haptics->regmap,
285				      drv2667_init_regs,
286				      ARRAY_SIZE(drv2667_init_regs));
287	if (error) {
288		dev_err(&haptics->client->dev,
289			"Failed to write init registers: %d\n",
290			error);
291		return error;
292	}
293
294	error = regmap_write(haptics->regmap, DRV2667_PAGE, haptics->page);
295	if (error) {
296		dev_err(&haptics->client->dev, "Failed to set page: %d\n",
297			error);
298		goto error_out;
299	}
300
301	error = drv2667_set_waveform_freq(haptics);
302	if (error)
303		goto error_page;
304
305	error = regmap_register_patch(haptics->regmap,
306				      drv2667_page1_init,
307				      ARRAY_SIZE(drv2667_page1_init));
308	if (error) {
309		dev_err(&haptics->client->dev,
310			"Failed to write page registers: %d\n",
311			error);
312		return error;
313	}
314
315	error = regmap_write(haptics->regmap, DRV2667_PAGE, DRV2667_PAGE_0);
316	return error;
317
318error_page:
319	regmap_write(haptics->regmap, DRV2667_PAGE, DRV2667_PAGE_0);
320error_out:
321	return error;
322}
323
324static const struct regmap_config drv2667_regmap_config = {
325	.reg_bits = 8,
326	.val_bits = 8,
327
328	.max_register = DRV2667_MAX_REG,
329	.reg_defaults = drv2667_reg_defs,
330	.num_reg_defaults = ARRAY_SIZE(drv2667_reg_defs),
331	.cache_type = REGCACHE_NONE,
332};
333
334static int drv2667_probe(struct i2c_client *client,
335			 const struct i2c_device_id *id)
336{
337	struct drv2667_data *haptics;
338	int error;
339
340	haptics = devm_kzalloc(&client->dev, sizeof(*haptics), GFP_KERNEL);
341	if (!haptics)
342		return -ENOMEM;
343
344	haptics->regulator = devm_regulator_get(&client->dev, "vbat");
345	if (IS_ERR(haptics->regulator)) {
346		error = PTR_ERR(haptics->regulator);
347		dev_err(&client->dev,
348			"unable to get regulator, error: %d\n", error);
349		return error;
350	}
351
352	haptics->input_dev = devm_input_allocate_device(&client->dev);
353	if (!haptics->input_dev) {
354		dev_err(&client->dev, "Failed to allocate input device\n");
355		return -ENOMEM;
356	}
357
358	haptics->input_dev->name = "drv2667:haptics";
359	haptics->input_dev->dev.parent = client->dev.parent;
360	haptics->input_dev->close = drv2667_close;
361	input_set_drvdata(haptics->input_dev, haptics);
362	input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
363
364	error = input_ff_create_memless(haptics->input_dev, NULL,
365					drv2667_haptics_play);
366	if (error) {
367		dev_err(&client->dev, "input_ff_create() failed: %d\n",
368			error);
369		return error;
370	}
371
372	INIT_WORK(&haptics->work, drv2667_worker);
373
374	haptics->client = client;
375	i2c_set_clientdata(client, haptics);
376
377	haptics->regmap = devm_regmap_init_i2c(client, &drv2667_regmap_config);
378	if (IS_ERR(haptics->regmap)) {
379		error = PTR_ERR(haptics->regmap);
380		dev_err(&client->dev, "Failed to allocate register map: %d\n",
381			error);
382		return error;
383	}
384
385	error = drv2667_init(haptics);
386	if (error) {
387		dev_err(&client->dev, "Device init failed: %d\n", error);
388		return error;
389	}
390
391	error = input_register_device(haptics->input_dev);
392	if (error) {
393		dev_err(&client->dev, "couldn't register input device: %d\n",
394			error);
395		return error;
396	}
397
398	return 0;
399}
400
401static int __maybe_unused drv2667_suspend(struct device *dev)
402{
403	struct drv2667_data *haptics = dev_get_drvdata(dev);
404	int ret = 0;
405
406	mutex_lock(&haptics->input_dev->mutex);
407
408	if (haptics->input_dev->users) {
409		ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
410					 DRV2667_STANDBY, DRV2667_STANDBY);
411		if (ret) {
412			dev_err(dev, "Failed to set standby mode\n");
413			regulator_disable(haptics->regulator);
414			goto out;
415		}
416
417		ret = regulator_disable(haptics->regulator);
418		if (ret) {
419			dev_err(dev, "Failed to disable regulator\n");
420			regmap_update_bits(haptics->regmap,
421					   DRV2667_CTRL_2,
422					   DRV2667_STANDBY, 0);
423		}
424	}
425out:
426	mutex_unlock(&haptics->input_dev->mutex);
427	return ret;
428}
429
430static int __maybe_unused drv2667_resume(struct device *dev)
431{
432	struct drv2667_data *haptics = dev_get_drvdata(dev);
433	int ret = 0;
434
435	mutex_lock(&haptics->input_dev->mutex);
436
437	if (haptics->input_dev->users) {
438		ret = regulator_enable(haptics->regulator);
439		if (ret) {
440			dev_err(dev, "Failed to enable regulator\n");
441			goto out;
442		}
443
444		ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
445					 DRV2667_STANDBY, 0);
446		if (ret) {
447			dev_err(dev, "Failed to unset standby mode\n");
448			regulator_disable(haptics->regulator);
449			goto out;
450		}
451
452	}
453
454out:
455	mutex_unlock(&haptics->input_dev->mutex);
456	return ret;
457}
458
459static SIMPLE_DEV_PM_OPS(drv2667_pm_ops, drv2667_suspend, drv2667_resume);
460
461static const struct i2c_device_id drv2667_id[] = {
462	{ "drv2667", 0 },
463	{ }
464};
465MODULE_DEVICE_TABLE(i2c, drv2667_id);
466
467#ifdef CONFIG_OF
468static const struct of_device_id drv2667_of_match[] = {
469	{ .compatible = "ti,drv2667", },
470	{ }
471};
472MODULE_DEVICE_TABLE(of, drv2667_of_match);
473#endif
474
475static struct i2c_driver drv2667_driver = {
476	.probe		= drv2667_probe,
477	.driver		= {
478		.name	= "drv2667-haptics",
479		.of_match_table = of_match_ptr(drv2667_of_match),
480		.pm	= &drv2667_pm_ops,
481	},
482	.id_table = drv2667_id,
483};
484module_i2c_driver(drv2667_driver);
485
486MODULE_DESCRIPTION("TI DRV2667 haptics driver");
487MODULE_LICENSE("GPL");
488MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");