| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
 | /*
 *  Copyright (C) 2014 Motorola, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Adds ability to program periodic interrupts from user space that
 *  can wake the phone out of low power modes.
 *
 */
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/m4sensorhub.h>
#include <linux/slab.h>
#define m4ext_err(format, args...)  KDEBUG(M4SH_ERROR, format, ## args)
struct m4sensorhub_data *m4ext_m4;
DEFINE_MUTEX(m4ext_mutex);
uint8_t m4ext_display_status;
uint8_t m4ext_audio_status;
static void m4ext_panic_callback(struct m4sensorhub_data *m4, void *context)
{
	int err;
	mutex_lock(&m4ext_mutex);
	if (m4ext_m4 == NULL) {
		m4ext_err("%s: M4 data is NULL.\n", __func__);
		err = -ENODATA;
		goto m4ext_panic_callback_fail;
	}
	err = m4sensorhub_reg_write(m4, M4SH_REG_USERSETTINGS_SCREENSTATUS,
		(char *)&m4ext_display_status, m4sh_no_mask);
	if (err < 0)
		m4ext_err("%s: Screen status write failed (%d).\n",
			__func__, err);
	else if (err != 1)
		m4ext_err("%s: Screen status wrote %d bytes instead of 1.\n",
			__func__, err);
	err = m4sensorhub_reg_write(m4, M4SH_REG_USERSETTINGS_AUDIOSTATUS,
		(char *)&m4ext_audio_status, m4sh_no_mask);
	if (err < 0)
		m4ext_err("%s: Audio status write failed (%d).\n",
			__func__, err);
	else if (err != 1)
		m4ext_err("%s: Audio status wrote %d bytes instead of 1.\n",
			__func__, err);
m4ext_panic_callback_fail:
	mutex_unlock(&m4ext_mutex);
	return;
}
int m4sensorhub_extern_init(struct m4sensorhub_data *m4)
{
	int err;
	mutex_lock(&m4ext_mutex);
	err = m4sensorhub_panic_register(m4, PANICHDL_EXTERN_RESTORE,
		m4ext_panic_callback, NULL);
	if (err < 0) {
		m4ext_err("%s: Failed to register panic callback.\n", __func__);
		goto m4sensorhub_extern_init_fail;
	}
	m4ext_m4 = m4;
m4sensorhub_extern_init_fail:
	mutex_unlock(&m4ext_mutex);
	return err;
}
EXPORT_SYMBOL_GPL(m4sensorhub_extern_init);
int m4sensorhub_extern_set_display_status(uint8_t status)
{
	int err;
	mutex_lock(&m4ext_mutex);
	if (m4ext_m4 == NULL) {
		m4ext_err("%s: M4 data is NULL.\n", __func__);
		err = -ENODATA;
		goto m4sensorhub_extern_set_display_status_fail;
	}
	m4ext_display_status = status;
	err = m4sensorhub_reg_write(m4ext_m4,
		M4SH_REG_USERSETTINGS_SCREENSTATUS,
		(char *)&status, m4sh_no_mask);
	if (err < 0) {
		m4ext_err("%s: I2C write failed (%d).\n", __func__, err);
		goto m4sensorhub_extern_set_display_status_fail;
	} else if (err != 1) {
		m4ext_err("%s: Wrote %d bytes instead of 1.\n", __func__, err);
		err = -EINVAL;
		goto m4sensorhub_extern_set_display_status_fail;
	}
	err = 0;
m4sensorhub_extern_set_display_status_fail:
	mutex_unlock(&m4ext_mutex);
	return err;
}
EXPORT_SYMBOL_GPL(m4sensorhub_extern_set_display_status);
int m4sensorhub_extern_set_audio_status(uint8_t status)
{
	int err;
	mutex_lock(&m4ext_mutex);
	if (m4ext_m4 == NULL) {
		m4ext_err("%s: M4 data is NULL.\n", __func__);
		err = -ENODATA;
		goto m4sensorhub_extern_set_audio_status_fail;
	}
	m4ext_audio_status = status;
	err = m4sensorhub_reg_write(m4ext_m4,
		M4SH_REG_USERSETTINGS_AUDIOSTATUS,
		(char *)&status, m4sh_no_mask);
	if (err < 0) {
		m4ext_err("%s: I2C write failed (%d).\n", __func__, err);
		goto m4sensorhub_extern_set_audio_status_fail;
	} else if (err != 1) {
		m4ext_err("%s: Wrote %d bytes instead of 1.\n", __func__, err);
		err = -EINVAL;
		goto m4sensorhub_extern_set_audio_status_fail;
	}
	err = 0;
m4sensorhub_extern_set_audio_status_fail:
	mutex_unlock(&m4ext_mutex);
	return err;
}
EXPORT_SYMBOL_GPL(m4sensorhub_extern_set_audio_status);
 |