extcon.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. /*
  2. * drivers/extcon/extcon.c - External Connector (extcon) framework.
  3. *
  4. * External connector (extcon) class driver
  5. *
  6. * Copyright (C) 2015 Samsung Electronics
  7. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  8. *
  9. * Copyright (C) 2012 Samsung Electronics
  10. * Author: Donggeun Kim <dg77.kim@samsung.com>
  11. * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
  12. *
  13. * based on android/drivers/switch/switch_class.c
  14. * Copyright (C) 2008 Google, Inc.
  15. * Author: Mike Lockwood <lockwood@android.com>
  16. *
  17. * This software is licensed under the terms of the GNU General Public
  18. * License version 2, as published by the Free Software Foundation, and
  19. * may be copied, distributed, and modified under those terms.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/init.h>
  29. #include <linux/device.h>
  30. #include <linux/fs.h>
  31. #include <linux/err.h>
  32. #include <linux/of.h>
  33. #include <linux/slab.h>
  34. #include <linux/sysfs.h>
  35. #include "extcon.h"
  36. #define SUPPORTED_CABLE_MAX 32
  37. #define CABLE_NAME_MAX 30
  38. struct __extcon_info {
  39. unsigned int type;
  40. unsigned int id;
  41. const char *name;
  42. } extcon_info[] = {
  43. [EXTCON_NONE] = {
  44. .type = EXTCON_TYPE_MISC,
  45. .id = EXTCON_NONE,
  46. .name = "NONE",
  47. },
  48. /* USB external connector */
  49. [EXTCON_USB] = {
  50. .type = EXTCON_TYPE_USB,
  51. .id = EXTCON_USB,
  52. .name = "USB",
  53. },
  54. [EXTCON_USB_HOST] = {
  55. .type = EXTCON_TYPE_USB,
  56. .id = EXTCON_USB_HOST,
  57. .name = "USB-HOST",
  58. },
  59. /* Charging external connector */
  60. [EXTCON_CHG_USB_SDP] = {
  61. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  62. .id = EXTCON_CHG_USB_SDP,
  63. .name = "SDP",
  64. },
  65. [EXTCON_CHG_USB_DCP] = {
  66. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  67. .id = EXTCON_CHG_USB_DCP,
  68. .name = "DCP",
  69. },
  70. [EXTCON_CHG_USB_CDP] = {
  71. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  72. .id = EXTCON_CHG_USB_CDP,
  73. .name = "CDP",
  74. },
  75. [EXTCON_CHG_USB_ACA] = {
  76. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  77. .id = EXTCON_CHG_USB_ACA,
  78. .name = "ACA",
  79. },
  80. [EXTCON_CHG_USB_FAST] = {
  81. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  82. .id = EXTCON_CHG_USB_FAST,
  83. .name = "FAST-CHARGER",
  84. },
  85. [EXTCON_CHG_USB_SLOW] = {
  86. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  87. .id = EXTCON_CHG_USB_SLOW,
  88. .name = "SLOW-CHARGER",
  89. },
  90. [EXTCON_CHG_WPT] = {
  91. .type = EXTCON_TYPE_CHG,
  92. .id = EXTCON_CHG_WPT,
  93. .name = "WPT",
  94. },
  95. [EXTCON_CHG_USB_PD] = {
  96. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  97. .id = EXTCON_CHG_USB_PD,
  98. .name = "PD",
  99. },
  100. /* Jack external connector */
  101. [EXTCON_JACK_MICROPHONE] = {
  102. .type = EXTCON_TYPE_JACK,
  103. .id = EXTCON_JACK_MICROPHONE,
  104. .name = "MICROPHONE",
  105. },
  106. [EXTCON_JACK_HEADPHONE] = {
  107. .type = EXTCON_TYPE_JACK,
  108. .id = EXTCON_JACK_HEADPHONE,
  109. .name = "HEADPHONE",
  110. },
  111. [EXTCON_JACK_LINE_IN] = {
  112. .type = EXTCON_TYPE_JACK,
  113. .id = EXTCON_JACK_LINE_IN,
  114. .name = "LINE-IN",
  115. },
  116. [EXTCON_JACK_LINE_OUT] = {
  117. .type = EXTCON_TYPE_JACK,
  118. .id = EXTCON_JACK_LINE_OUT,
  119. .name = "LINE-OUT",
  120. },
  121. [EXTCON_JACK_VIDEO_IN] = {
  122. .type = EXTCON_TYPE_JACK,
  123. .id = EXTCON_JACK_VIDEO_IN,
  124. .name = "VIDEO-IN",
  125. },
  126. [EXTCON_JACK_VIDEO_OUT] = {
  127. .type = EXTCON_TYPE_JACK,
  128. .id = EXTCON_JACK_VIDEO_OUT,
  129. .name = "VIDEO-OUT",
  130. },
  131. [EXTCON_JACK_SPDIF_IN] = {
  132. .type = EXTCON_TYPE_JACK,
  133. .id = EXTCON_JACK_SPDIF_IN,
  134. .name = "SPDIF-IN",
  135. },
  136. [EXTCON_JACK_SPDIF_OUT] = {
  137. .type = EXTCON_TYPE_JACK,
  138. .id = EXTCON_JACK_SPDIF_OUT,
  139. .name = "SPDIF-OUT",
  140. },
  141. /* Display external connector */
  142. [EXTCON_DISP_HDMI] = {
  143. .type = EXTCON_TYPE_DISP,
  144. .id = EXTCON_DISP_HDMI,
  145. .name = "HDMI",
  146. },
  147. [EXTCON_DISP_MHL] = {
  148. .type = EXTCON_TYPE_DISP,
  149. .id = EXTCON_DISP_MHL,
  150. .name = "MHL",
  151. },
  152. [EXTCON_DISP_DVI] = {
  153. .type = EXTCON_TYPE_DISP,
  154. .id = EXTCON_DISP_DVI,
  155. .name = "DVI",
  156. },
  157. [EXTCON_DISP_VGA] = {
  158. .type = EXTCON_TYPE_DISP,
  159. .id = EXTCON_DISP_VGA,
  160. .name = "VGA",
  161. },
  162. [EXTCON_DISP_DP] = {
  163. .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
  164. .id = EXTCON_DISP_DP,
  165. .name = "DP",
  166. },
  167. [EXTCON_DISP_HMD] = {
  168. .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
  169. .id = EXTCON_DISP_HMD,
  170. .name = "HMD",
  171. },
  172. /* Miscellaneous external connector */
  173. [EXTCON_DOCK] = {
  174. .type = EXTCON_TYPE_MISC,
  175. .id = EXTCON_DOCK,
  176. .name = "DOCK",
  177. },
  178. [EXTCON_JIG] = {
  179. .type = EXTCON_TYPE_MISC,
  180. .id = EXTCON_JIG,
  181. .name = "JIG",
  182. },
  183. [EXTCON_MECHANICAL] = {
  184. .type = EXTCON_TYPE_MISC,
  185. .id = EXTCON_MECHANICAL,
  186. .name = "MECHANICAL",
  187. },
  188. { /* sentinel */ }
  189. };
  190. /**
  191. * struct extcon_cable - An internal data for each cable of extcon device.
  192. * @edev: The extcon device
  193. * @cable_index: Index of this cable in the edev
  194. * @attr_g: Attribute group for the cable
  195. * @attr_name: "name" sysfs entry
  196. * @attr_state: "state" sysfs entry
  197. * @attrs: Array pointing to attr_name and attr_state for attr_g
  198. */
  199. struct extcon_cable {
  200. struct extcon_dev *edev;
  201. int cable_index;
  202. struct attribute_group attr_g;
  203. struct device_attribute attr_name;
  204. struct device_attribute attr_state;
  205. struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
  206. union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
  207. union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
  208. union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
  209. union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
  210. unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
  211. unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
  212. unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
  213. unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
  214. };
  215. static struct class *extcon_class;
  216. #if defined(CONFIG_ANDROID)
  217. static struct class_compat *switch_class;
  218. #endif /* CONFIG_ANDROID */
  219. static LIST_HEAD(extcon_dev_list);
  220. static DEFINE_MUTEX(extcon_dev_list_lock);
  221. /**
  222. * check_mutually_exclusive - Check if new_state violates mutually_exclusive
  223. * condition.
  224. * @edev: the extcon device
  225. * @new_state: new cable attach status for @edev
  226. *
  227. * Returns 0 if nothing violates. Returns the index + 1 for the first
  228. * violated condition.
  229. */
  230. static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
  231. {
  232. int i = 0;
  233. if (!edev->mutually_exclusive)
  234. return 0;
  235. for (i = 0; edev->mutually_exclusive[i]; i++) {
  236. int weight;
  237. u32 correspondants = new_state & edev->mutually_exclusive[i];
  238. /* calculate the total number of bits set */
  239. weight = hweight32(correspondants);
  240. if (weight > 1)
  241. return i + 1;
  242. }
  243. return 0;
  244. }
  245. static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
  246. {
  247. int i;
  248. /* Find the the index of extcon cable in edev->supported_cable */
  249. for (i = 0; i < edev->max_supported; i++) {
  250. if (edev->supported_cable[i] == id)
  251. return i;
  252. }
  253. return -EINVAL;
  254. }
  255. static int get_extcon_type(unsigned int prop)
  256. {
  257. switch (prop) {
  258. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  259. return EXTCON_TYPE_USB;
  260. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  261. return EXTCON_TYPE_CHG;
  262. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  263. return EXTCON_TYPE_JACK;
  264. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  265. return EXTCON_TYPE_DISP;
  266. default:
  267. return -EINVAL;
  268. }
  269. }
  270. static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
  271. {
  272. return !!(edev->state & BIT(index));
  273. }
  274. static bool is_extcon_changed(struct extcon_dev *edev, int index,
  275. bool new_state)
  276. {
  277. int state = !!(edev->state & BIT(index));
  278. return (state != new_state);
  279. }
  280. static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
  281. {
  282. int type;
  283. /* Check whether the property is supported or not. */
  284. type = get_extcon_type(prop);
  285. if (type < 0)
  286. return false;
  287. /* Check whether a specific extcon id supports the property or not. */
  288. return !!(extcon_info[id].type & type);
  289. }
  290. static int is_extcon_property_capability(struct extcon_dev *edev,
  291. unsigned int id, int index,unsigned int prop)
  292. {
  293. struct extcon_cable *cable;
  294. int type, ret;
  295. /* Check whether the property is supported or not. */
  296. type = get_extcon_type(prop);
  297. if (type < 0)
  298. return type;
  299. cable = &edev->cables[index];
  300. switch (type) {
  301. case EXTCON_TYPE_USB:
  302. ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
  303. break;
  304. case EXTCON_TYPE_CHG:
  305. ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
  306. break;
  307. case EXTCON_TYPE_JACK:
  308. ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
  309. break;
  310. case EXTCON_TYPE_DISP:
  311. ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
  312. break;
  313. default:
  314. ret = -EINVAL;
  315. }
  316. return ret;
  317. }
  318. static void init_property(struct extcon_dev *edev, unsigned int id, int index)
  319. {
  320. unsigned int type = extcon_info[id].type;
  321. struct extcon_cable *cable = &edev->cables[index];
  322. if (EXTCON_TYPE_USB & type)
  323. memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
  324. if (EXTCON_TYPE_CHG & type)
  325. memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
  326. if (EXTCON_TYPE_JACK & type)
  327. memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
  328. if (EXTCON_TYPE_DISP & type)
  329. memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
  330. }
  331. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  332. char *buf)
  333. {
  334. int i, count = 0;
  335. struct extcon_dev *edev = dev_get_drvdata(dev);
  336. if (edev->max_supported == 0)
  337. return sprintf(buf, "%u\n", edev->state);
  338. for (i = 0; i < edev->max_supported; i++) {
  339. count += sprintf(buf + count, "%s=%d\n",
  340. extcon_info[edev->supported_cable[i]].name,
  341. !!(edev->state & (1 << i)));
  342. }
  343. return count;
  344. }
  345. static DEVICE_ATTR_RO(state);
  346. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  347. char *buf)
  348. {
  349. struct extcon_dev *edev = dev_get_drvdata(dev);
  350. return sprintf(buf, "%s\n", edev->name);
  351. }
  352. static DEVICE_ATTR_RO(name);
  353. static ssize_t cable_name_show(struct device *dev,
  354. struct device_attribute *attr, char *buf)
  355. {
  356. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  357. attr_name);
  358. int i = cable->cable_index;
  359. return sprintf(buf, "%s\n",
  360. extcon_info[cable->edev->supported_cable[i]].name);
  361. }
  362. static ssize_t cable_state_show(struct device *dev,
  363. struct device_attribute *attr, char *buf)
  364. {
  365. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  366. attr_state);
  367. int i = cable->cable_index;
  368. return sprintf(buf, "%d\n",
  369. extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
  370. }
  371. /**
  372. * extcon_sync() - Synchronize the states for both the attached/detached
  373. * @edev: the extcon device that has the cable.
  374. *
  375. * This function send a notification to synchronize the all states of a
  376. * specific external connector
  377. */
  378. int extcon_sync(struct extcon_dev *edev, unsigned int id)
  379. {
  380. char name_buf[120];
  381. char state_buf[120];
  382. char *prop_buf;
  383. char *envp[3];
  384. int env_offset = 0;
  385. int length;
  386. int index;
  387. int state;
  388. unsigned long flags;
  389. if (!edev)
  390. return -EINVAL;
  391. index = find_cable_index_by_id(edev, id);
  392. if (index < 0)
  393. return index;
  394. spin_lock_irqsave(&edev->lock, flags);
  395. state = !!(edev->state & BIT(index));
  396. raw_notifier_call_chain(&edev->nh[index], state, edev);
  397. /* This could be in interrupt handler */
  398. prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
  399. if (!prop_buf) {
  400. /* Unlock early before uevent */
  401. spin_unlock_irqrestore(&edev->lock, flags);
  402. dev_err(&edev->dev, "out of memory in extcon_set_state\n");
  403. kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
  404. return -ENOMEM;
  405. }
  406. length = name_show(&edev->dev, NULL, prop_buf);
  407. if (length > 0) {
  408. if (prop_buf[length - 1] == '\n')
  409. prop_buf[length - 1] = 0;
  410. snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
  411. envp[env_offset++] = name_buf;
  412. }
  413. length = state_show(&edev->dev, NULL, prop_buf);
  414. if (length > 0) {
  415. if (prop_buf[length - 1] == '\n')
  416. prop_buf[length - 1] = 0;
  417. snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
  418. envp[env_offset++] = state_buf;
  419. }
  420. envp[env_offset] = NULL;
  421. /* Unlock early before uevent */
  422. spin_unlock_irqrestore(&edev->lock, flags);
  423. kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
  424. free_page((unsigned long)prop_buf);
  425. return 0;
  426. }
  427. EXPORT_SYMBOL_GPL(extcon_sync);
  428. /**
  429. * extcon_get_state() - Get the state of a external connector.
  430. * @edev: the extcon device that has the cable.
  431. * @id: the unique id of each external connector in extcon enumeration.
  432. */
  433. int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
  434. {
  435. int index, state;
  436. unsigned long flags;
  437. if (!edev)
  438. return -EINVAL;
  439. index = find_cable_index_by_id(edev, id);
  440. if (index < 0)
  441. return index;
  442. spin_lock_irqsave(&edev->lock, flags);
  443. state = is_extcon_attached(edev, index);
  444. spin_unlock_irqrestore(&edev->lock, flags);
  445. return state;
  446. }
  447. EXPORT_SYMBOL_GPL(extcon_get_state);
  448. /**
  449. * extcon_set_state() - Set the state of a external connector.
  450. * without a notification.
  451. * @edev: the extcon device that has the cable.
  452. * @id: the unique id of each external connector
  453. * in extcon enumeration.
  454. * @state: the new cable status. The default semantics is
  455. * true: attached / false: detached.
  456. *
  457. * This function only set the state of a external connector without
  458. * a notification. To synchronize the data of a external connector,
  459. * use extcon_set_state_sync() and extcon_sync().
  460. */
  461. int extcon_set_state(struct extcon_dev *edev, unsigned int id,
  462. bool cable_state)
  463. {
  464. unsigned long flags;
  465. int index, ret = 0;
  466. if (!edev)
  467. return -EINVAL;
  468. index = find_cable_index_by_id(edev, id);
  469. if (index < 0)
  470. return index;
  471. spin_lock_irqsave(&edev->lock, flags);
  472. /* Check whether the external connector's state is changed. */
  473. if (!is_extcon_changed(edev, index, cable_state))
  474. goto out;
  475. if (check_mutually_exclusive(edev,
  476. (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
  477. ret = -EPERM;
  478. goto out;
  479. }
  480. /*
  481. * Initialize the value of extcon property before setting
  482. * the detached state for an external connector.
  483. */
  484. if (!cable_state)
  485. init_property(edev, id, index);
  486. /* Update the state for a external connector. */
  487. if (cable_state)
  488. edev->state |= BIT(index);
  489. else
  490. edev->state &= ~(BIT(index));
  491. out:
  492. spin_unlock_irqrestore(&edev->lock, flags);
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(extcon_set_state);
  496. /**
  497. * extcon_set_state_sync() - Set the state of a external connector
  498. * with a notification.
  499. * @edev: the extcon device that has the cable.
  500. * @id: the unique id of each external connector
  501. * in extcon enumeration.
  502. * @state: the new cable status. The default semantics is
  503. * true: attached / false: detached.
  504. *
  505. * This function set the state of external connector and synchronize the data
  506. * by usning a notification.
  507. */
  508. int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
  509. bool cable_state)
  510. {
  511. int ret, index;
  512. unsigned long flags;
  513. index = find_cable_index_by_id(edev, id);
  514. if (index < 0)
  515. return index;
  516. /* Check whether the external connector's state is changed. */
  517. spin_lock_irqsave(&edev->lock, flags);
  518. ret = is_extcon_changed(edev, index, cable_state);
  519. spin_unlock_irqrestore(&edev->lock, flags);
  520. if (!ret)
  521. return 0;
  522. ret = extcon_set_state(edev, id, cable_state);
  523. if (ret < 0)
  524. return ret;
  525. return extcon_sync(edev, id);
  526. }
  527. EXPORT_SYMBOL_GPL(extcon_set_state_sync);
  528. /**
  529. * extcon_get_property() - Get the property value of a specific cable.
  530. * @edev: the extcon device that has the cable.
  531. * @id: the unique id of each external connector
  532. * in extcon enumeration.
  533. * @prop: the property id among enum extcon_property.
  534. * @prop_val: the pointer which store the value of property.
  535. *
  536. * When getting the property value of external connector, the external connector
  537. * should be attached. If detached state, function just return 0 without
  538. * property value. Also, the each property should be included in the list of
  539. * supported properties according to the type of external connectors.
  540. *
  541. * Returns 0 if success or error number if fail
  542. */
  543. int extcon_get_property(struct extcon_dev *edev, unsigned int id,
  544. unsigned int prop,
  545. union extcon_property_value *prop_val)
  546. {
  547. struct extcon_cable *cable;
  548. unsigned long flags;
  549. int index, ret = 0;
  550. *prop_val = (union extcon_property_value)(0);
  551. if (!edev)
  552. return -EINVAL;
  553. /* Check whether the property is supported or not */
  554. if (!is_extcon_property_supported(id, prop))
  555. return -EINVAL;
  556. /* Find the cable index of external connector by using id */
  557. index = find_cable_index_by_id(edev, id);
  558. if (index < 0)
  559. return index;
  560. spin_lock_irqsave(&edev->lock, flags);
  561. /* Check whether the property is available or not. */
  562. if (!is_extcon_property_capability(edev, id, index, prop)) {
  563. spin_unlock_irqrestore(&edev->lock, flags);
  564. return -EPERM;
  565. }
  566. /*
  567. * Check whether the external connector is attached.
  568. * If external connector is detached, the user can not
  569. * get the property value.
  570. */
  571. if (!is_extcon_attached(edev, index)) {
  572. spin_unlock_irqrestore(&edev->lock, flags);
  573. return 0;
  574. }
  575. cable = &edev->cables[index];
  576. /* Get the property value according to extcon type */
  577. switch (prop) {
  578. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  579. *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
  580. break;
  581. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  582. *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
  583. break;
  584. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  585. *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
  586. break;
  587. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  588. *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
  589. break;
  590. default:
  591. ret = -EINVAL;
  592. break;
  593. }
  594. spin_unlock_irqrestore(&edev->lock, flags);
  595. return ret;
  596. }
  597. EXPORT_SYMBOL_GPL(extcon_get_property);
  598. /**
  599. * extcon_set_property() - Set the property value of a specific cable.
  600. * @edev: the extcon device that has the cable.
  601. * @id: the unique id of each external connector
  602. * in extcon enumeration.
  603. * @prop: the property id among enum extcon_property.
  604. * @prop_val: the pointer including the new value of property.
  605. *
  606. * The each property should be included in the list of supported properties
  607. * according to the type of external connectors.
  608. *
  609. * Returns 0 if success or error number if fail
  610. */
  611. int extcon_set_property(struct extcon_dev *edev, unsigned int id,
  612. unsigned int prop,
  613. union extcon_property_value prop_val)
  614. {
  615. struct extcon_cable *cable;
  616. unsigned long flags;
  617. int index, ret = 0;
  618. if (!edev)
  619. return -EINVAL;
  620. /* Check whether the property is supported or not */
  621. if (!is_extcon_property_supported(id, prop))
  622. return -EINVAL;
  623. /* Find the cable index of external connector by using id */
  624. index = find_cable_index_by_id(edev, id);
  625. if (index < 0)
  626. return index;
  627. spin_lock_irqsave(&edev->lock, flags);
  628. /* Check whether the property is available or not. */
  629. if (!is_extcon_property_capability(edev, id, index, prop)) {
  630. spin_unlock_irqrestore(&edev->lock, flags);
  631. return -EPERM;
  632. }
  633. cable = &edev->cables[index];
  634. /* Set the property value according to extcon type */
  635. switch (prop) {
  636. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  637. cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
  638. break;
  639. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  640. cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
  641. break;
  642. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  643. cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
  644. break;
  645. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  646. cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
  647. break;
  648. default:
  649. ret = -EINVAL;
  650. break;
  651. }
  652. spin_unlock_irqrestore(&edev->lock, flags);
  653. return ret;
  654. }
  655. EXPORT_SYMBOL_GPL(extcon_set_property);
  656. /**
  657. * extcon_set_property_sync() - Set the property value of a specific cable
  658. with a notification.
  659. * @prop_val: the pointer including the new value of property.
  660. *
  661. * When setting the property value of external connector, the external connector
  662. * should be attached. The each property should be included in the list of
  663. * supported properties according to the type of external connectors.
  664. *
  665. * Returns 0 if success or error number if fail
  666. */
  667. int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
  668. unsigned int prop,
  669. union extcon_property_value prop_val)
  670. {
  671. int ret;
  672. ret = extcon_set_property(edev, id, prop, prop_val);
  673. if (ret < 0)
  674. return ret;
  675. return extcon_sync(edev, id);
  676. }
  677. EXPORT_SYMBOL_GPL(extcon_set_property_sync);
  678. /**
  679. * extcon_get_property_capability() - Get the capability of property
  680. * of an external connector.
  681. * @edev: the extcon device that has the cable.
  682. * @id: the unique id of each external connector
  683. * in extcon enumeration.
  684. * @prop: the property id among enum extcon_property.
  685. *
  686. * Returns 1 if the property is available or 0 if not available.
  687. */
  688. int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
  689. unsigned int prop)
  690. {
  691. int index;
  692. if (!edev)
  693. return -EINVAL;
  694. /* Check whether the property is supported or not */
  695. if (!is_extcon_property_supported(id, prop))
  696. return -EINVAL;
  697. /* Find the cable index of external connector by using id */
  698. index = find_cable_index_by_id(edev, id);
  699. if (index < 0)
  700. return index;
  701. return is_extcon_property_capability(edev, id, index, prop);
  702. }
  703. EXPORT_SYMBOL_GPL(extcon_get_property_capability);
  704. /**
  705. * extcon_set_property_capability() - Set the capability of a property
  706. * of an external connector.
  707. * @edev: the extcon device that has the cable.
  708. * @id: the unique id of each external connector
  709. * in extcon enumeration.
  710. * @prop: the property id among enum extcon_property.
  711. *
  712. * This function set the capability of a property for an external connector
  713. * to mark the bit in capability bitmap which mean the available state of
  714. * a property.
  715. *
  716. * Returns 0 if success or error number if fail
  717. */
  718. int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
  719. unsigned int prop)
  720. {
  721. struct extcon_cable *cable;
  722. int index, type, ret = 0;
  723. if (!edev)
  724. return -EINVAL;
  725. /* Check whether the property is supported or not. */
  726. if (!is_extcon_property_supported(id, prop))
  727. return -EINVAL;
  728. /* Find the cable index of external connector by using id. */
  729. index = find_cable_index_by_id(edev, id);
  730. if (index < 0)
  731. return index;
  732. type = get_extcon_type(prop);
  733. if (type < 0)
  734. return type;
  735. cable = &edev->cables[index];
  736. switch (type) {
  737. case EXTCON_TYPE_USB:
  738. __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
  739. break;
  740. case EXTCON_TYPE_CHG:
  741. __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
  742. break;
  743. case EXTCON_TYPE_JACK:
  744. __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
  745. break;
  746. case EXTCON_TYPE_DISP:
  747. __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
  748. break;
  749. default:
  750. ret = -EINVAL;
  751. }
  752. return ret;
  753. }
  754. EXPORT_SYMBOL_GPL(extcon_set_property_capability);
  755. /**
  756. * extcon_get_extcon_dev() - Get the extcon device instance from the name
  757. * @extcon_name: The extcon name provided with extcon_dev_register()
  758. */
  759. struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
  760. {
  761. struct extcon_dev *sd;
  762. if (!extcon_name)
  763. return ERR_PTR(-EINVAL);
  764. mutex_lock(&extcon_dev_list_lock);
  765. list_for_each_entry(sd, &extcon_dev_list, entry) {
  766. if (!strcmp(sd->name, extcon_name))
  767. goto out;
  768. }
  769. sd = NULL;
  770. out:
  771. mutex_unlock(&extcon_dev_list_lock);
  772. return sd;
  773. }
  774. EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
  775. /**
  776. * extcon_register_notifier() - Register a notifiee to get notified by
  777. * any attach status changes from the extcon.
  778. * @edev: the extcon device that has the external connecotr.
  779. * @id: the unique id of each external connector in extcon enumeration.
  780. * @nb: a notifier block to be registered.
  781. *
  782. * Note that the second parameter given to the callback of nb (val) is
  783. * "old_state", not the current state. The current state can be retrieved
  784. * by looking at the third pameter (edev pointer)'s state value.
  785. */
  786. int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
  787. struct notifier_block *nb)
  788. {
  789. unsigned long flags;
  790. int ret, idx = -EINVAL;
  791. if (!edev || !nb)
  792. return -EINVAL;
  793. idx = find_cable_index_by_id(edev, id);
  794. if (idx < 0)
  795. return idx;
  796. spin_lock_irqsave(&edev->lock, flags);
  797. ret = raw_notifier_chain_register(&edev->nh[idx], nb);
  798. spin_unlock_irqrestore(&edev->lock, flags);
  799. return ret;
  800. }
  801. EXPORT_SYMBOL_GPL(extcon_register_notifier);
  802. /**
  803. * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
  804. * @edev: the extcon device that has the external connecotr.
  805. * @id: the unique id of each external connector in extcon enumeration.
  806. * @nb: a notifier block to be registered.
  807. */
  808. int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
  809. struct notifier_block *nb)
  810. {
  811. unsigned long flags;
  812. int ret, idx;
  813. if (!edev || !nb)
  814. return -EINVAL;
  815. idx = find_cable_index_by_id(edev, id);
  816. if (idx < 0)
  817. return idx;
  818. spin_lock_irqsave(&edev->lock, flags);
  819. ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
  820. spin_unlock_irqrestore(&edev->lock, flags);
  821. return ret;
  822. }
  823. EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
  824. static struct attribute *extcon_attrs[] = {
  825. &dev_attr_state.attr,
  826. &dev_attr_name.attr,
  827. NULL,
  828. };
  829. ATTRIBUTE_GROUPS(extcon);
  830. static int create_extcon_class(void)
  831. {
  832. if (!extcon_class) {
  833. extcon_class = class_create(THIS_MODULE, "extcon");
  834. if (IS_ERR(extcon_class))
  835. return PTR_ERR(extcon_class);
  836. extcon_class->dev_groups = extcon_groups;
  837. #if defined(CONFIG_ANDROID)
  838. switch_class = class_compat_register("switch");
  839. if (WARN(!switch_class, "cannot allocate"))
  840. return -ENOMEM;
  841. #endif /* CONFIG_ANDROID */
  842. }
  843. return 0;
  844. }
  845. static void extcon_dev_release(struct device *dev)
  846. {
  847. }
  848. static const char *muex_name = "mutually_exclusive";
  849. static void dummy_sysfs_dev_release(struct device *dev)
  850. {
  851. }
  852. /*
  853. * extcon_dev_allocate() - Allocate the memory of extcon device.
  854. * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
  855. * If supported_cable is NULL, cable name related APIs
  856. * are disabled.
  857. *
  858. * This function allocates the memory for extcon device without allocating
  859. * memory in each extcon provider driver and initialize default setting for
  860. * extcon device.
  861. *
  862. * Return the pointer of extcon device if success or ERR_PTR(err) if fail
  863. */
  864. struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
  865. {
  866. struct extcon_dev *edev;
  867. if (!supported_cable)
  868. return ERR_PTR(-EINVAL);
  869. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  870. if (!edev)
  871. return ERR_PTR(-ENOMEM);
  872. edev->max_supported = 0;
  873. edev->supported_cable = supported_cable;
  874. return edev;
  875. }
  876. /*
  877. * extcon_dev_free() - Free the memory of extcon device.
  878. * @edev: the extcon device to free
  879. */
  880. void extcon_dev_free(struct extcon_dev *edev)
  881. {
  882. kfree(edev);
  883. }
  884. EXPORT_SYMBOL_GPL(extcon_dev_free);
  885. /**
  886. * extcon_dev_register() - Register a new extcon device
  887. * @edev : the new extcon device (should be allocated before calling)
  888. *
  889. * Among the members of edev struct, please set the "user initializing data"
  890. * in any case and set the "optional callbacks" if required. However, please
  891. * do not set the values of "internal data", which are initialized by
  892. * this function.
  893. */
  894. int extcon_dev_register(struct extcon_dev *edev)
  895. {
  896. int ret, index = 0;
  897. static atomic_t edev_no = ATOMIC_INIT(-1);
  898. if (!extcon_class) {
  899. ret = create_extcon_class();
  900. if (ret < 0)
  901. return ret;
  902. }
  903. if (!edev || !edev->supported_cable)
  904. return -EINVAL;
  905. for (; edev->supported_cable[index] != EXTCON_NONE; index++);
  906. edev->max_supported = index;
  907. if (index > SUPPORTED_CABLE_MAX) {
  908. dev_err(&edev->dev,
  909. "exceed the maximum number of supported cables\n");
  910. return -EINVAL;
  911. }
  912. edev->dev.class = extcon_class;
  913. edev->dev.release = extcon_dev_release;
  914. edev->name = dev_name(edev->dev.parent);
  915. if (IS_ERR_OR_NULL(edev->name)) {
  916. dev_err(&edev->dev,
  917. "extcon device name is null\n");
  918. return -EINVAL;
  919. }
  920. dev_set_name(&edev->dev, "extcon%lu",
  921. (unsigned long)atomic_inc_return(&edev_no));
  922. if (edev->max_supported) {
  923. char buf[10];
  924. char *str;
  925. struct extcon_cable *cable;
  926. edev->cables = kzalloc(sizeof(struct extcon_cable) *
  927. edev->max_supported, GFP_KERNEL);
  928. if (!edev->cables) {
  929. ret = -ENOMEM;
  930. goto err_sysfs_alloc;
  931. }
  932. for (index = 0; index < edev->max_supported; index++) {
  933. cable = &edev->cables[index];
  934. snprintf(buf, 10, "cable.%d", index);
  935. str = kzalloc(sizeof(char) * (strlen(buf) + 1),
  936. GFP_KERNEL);
  937. if (!str) {
  938. for (index--; index >= 0; index--) {
  939. cable = &edev->cables[index];
  940. kfree(cable->attr_g.name);
  941. }
  942. ret = -ENOMEM;
  943. goto err_alloc_cables;
  944. }
  945. strcpy(str, buf);
  946. cable->edev = edev;
  947. cable->cable_index = index;
  948. cable->attrs[0] = &cable->attr_name.attr;
  949. cable->attrs[1] = &cable->attr_state.attr;
  950. cable->attrs[2] = NULL;
  951. cable->attr_g.name = str;
  952. cable->attr_g.attrs = cable->attrs;
  953. sysfs_attr_init(&cable->attr_name.attr);
  954. cable->attr_name.attr.name = "name";
  955. cable->attr_name.attr.mode = 0444;
  956. cable->attr_name.show = cable_name_show;
  957. sysfs_attr_init(&cable->attr_state.attr);
  958. cable->attr_state.attr.name = "state";
  959. cable->attr_state.attr.mode = 0444;
  960. cable->attr_state.show = cable_state_show;
  961. }
  962. }
  963. if (edev->max_supported && edev->mutually_exclusive) {
  964. char buf[80];
  965. char *name;
  966. /* Count the size of mutually_exclusive array */
  967. for (index = 0; edev->mutually_exclusive[index]; index++)
  968. ;
  969. edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
  970. (index + 1), GFP_KERNEL);
  971. if (!edev->attrs_muex) {
  972. ret = -ENOMEM;
  973. goto err_muex;
  974. }
  975. edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
  976. index, GFP_KERNEL);
  977. if (!edev->d_attrs_muex) {
  978. ret = -ENOMEM;
  979. kfree(edev->attrs_muex);
  980. goto err_muex;
  981. }
  982. for (index = 0; edev->mutually_exclusive[index]; index++) {
  983. sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
  984. name = kzalloc(sizeof(char) * (strlen(buf) + 1),
  985. GFP_KERNEL);
  986. if (!name) {
  987. for (index--; index >= 0; index--) {
  988. kfree(edev->d_attrs_muex[index].attr.
  989. name);
  990. }
  991. kfree(edev->d_attrs_muex);
  992. kfree(edev->attrs_muex);
  993. ret = -ENOMEM;
  994. goto err_muex;
  995. }
  996. strcpy(name, buf);
  997. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  998. edev->d_attrs_muex[index].attr.name = name;
  999. edev->d_attrs_muex[index].attr.mode = 0000;
  1000. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  1001. .attr;
  1002. }
  1003. edev->attr_g_muex.name = muex_name;
  1004. edev->attr_g_muex.attrs = edev->attrs_muex;
  1005. }
  1006. if (edev->max_supported) {
  1007. edev->extcon_dev_type.groups =
  1008. kzalloc(sizeof(struct attribute_group *) *
  1009. (edev->max_supported + 2), GFP_KERNEL);
  1010. if (!edev->extcon_dev_type.groups) {
  1011. ret = -ENOMEM;
  1012. goto err_alloc_groups;
  1013. }
  1014. edev->extcon_dev_type.name = dev_name(&edev->dev);
  1015. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  1016. for (index = 0; index < edev->max_supported; index++)
  1017. edev->extcon_dev_type.groups[index] =
  1018. &edev->cables[index].attr_g;
  1019. if (edev->mutually_exclusive)
  1020. edev->extcon_dev_type.groups[index] =
  1021. &edev->attr_g_muex;
  1022. edev->dev.type = &edev->extcon_dev_type;
  1023. }
  1024. ret = device_register(&edev->dev);
  1025. if (ret) {
  1026. put_device(&edev->dev);
  1027. goto err_dev;
  1028. }
  1029. #if defined(CONFIG_ANDROID)
  1030. if (switch_class)
  1031. ret = class_compat_create_link(switch_class, &edev->dev, NULL);
  1032. #endif /* CONFIG_ANDROID */
  1033. spin_lock_init(&edev->lock);
  1034. edev->nh = devm_kzalloc(&edev->dev,
  1035. sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
  1036. if (!edev->nh) {
  1037. ret = -ENOMEM;
  1038. goto err_dev;
  1039. }
  1040. for (index = 0; index < edev->max_supported; index++)
  1041. RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
  1042. dev_set_drvdata(&edev->dev, edev);
  1043. edev->state = 0;
  1044. mutex_lock(&extcon_dev_list_lock);
  1045. list_add(&edev->entry, &extcon_dev_list);
  1046. mutex_unlock(&extcon_dev_list_lock);
  1047. return 0;
  1048. err_dev:
  1049. if (edev->max_supported)
  1050. kfree(edev->extcon_dev_type.groups);
  1051. err_alloc_groups:
  1052. if (edev->max_supported && edev->mutually_exclusive) {
  1053. for (index = 0; edev->mutually_exclusive[index]; index++)
  1054. kfree(edev->d_attrs_muex[index].attr.name);
  1055. kfree(edev->d_attrs_muex);
  1056. kfree(edev->attrs_muex);
  1057. }
  1058. err_muex:
  1059. for (index = 0; index < edev->max_supported; index++)
  1060. kfree(edev->cables[index].attr_g.name);
  1061. err_alloc_cables:
  1062. if (edev->max_supported)
  1063. kfree(edev->cables);
  1064. err_sysfs_alloc:
  1065. return ret;
  1066. }
  1067. EXPORT_SYMBOL_GPL(extcon_dev_register);
  1068. /**
  1069. * extcon_dev_unregister() - Unregister the extcon device.
  1070. * @edev: the extcon device instance to be unregistered.
  1071. *
  1072. * Note that this does not call kfree(edev) because edev was not allocated
  1073. * by this class.
  1074. */
  1075. void extcon_dev_unregister(struct extcon_dev *edev)
  1076. {
  1077. int index;
  1078. if (!edev)
  1079. return;
  1080. mutex_lock(&extcon_dev_list_lock);
  1081. list_del(&edev->entry);
  1082. mutex_unlock(&extcon_dev_list_lock);
  1083. if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
  1084. dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
  1085. dev_name(&edev->dev));
  1086. return;
  1087. }
  1088. device_unregister(&edev->dev);
  1089. if (edev->mutually_exclusive && edev->max_supported) {
  1090. for (index = 0; edev->mutually_exclusive[index];
  1091. index++)
  1092. kfree(edev->d_attrs_muex[index].attr.name);
  1093. kfree(edev->d_attrs_muex);
  1094. kfree(edev->attrs_muex);
  1095. }
  1096. for (index = 0; index < edev->max_supported; index++)
  1097. kfree(edev->cables[index].attr_g.name);
  1098. if (edev->max_supported) {
  1099. kfree(edev->extcon_dev_type.groups);
  1100. kfree(edev->cables);
  1101. }
  1102. #if defined(CONFIG_ANDROID)
  1103. if (switch_class)
  1104. class_compat_remove_link(switch_class, &edev->dev, NULL);
  1105. #endif
  1106. put_device(&edev->dev);
  1107. }
  1108. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  1109. #ifdef CONFIG_OF
  1110. /*
  1111. * extcon_get_edev_by_phandle - Get the extcon device from devicetree
  1112. * @dev - instance to the given device
  1113. * @index - index into list of extcon_dev
  1114. *
  1115. * return the instance of extcon device
  1116. */
  1117. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1118. {
  1119. struct device_node *node;
  1120. struct extcon_dev *edev;
  1121. if (!dev)
  1122. return ERR_PTR(-EINVAL);
  1123. if (!dev->of_node) {
  1124. dev_dbg(dev, "device does not have a device node entry\n");
  1125. return ERR_PTR(-EINVAL);
  1126. }
  1127. node = of_parse_phandle(dev->of_node, "extcon", index);
  1128. if (!node) {
  1129. dev_dbg(dev, "failed to get phandle in %s node\n",
  1130. dev->of_node->full_name);
  1131. return ERR_PTR(-ENODEV);
  1132. }
  1133. mutex_lock(&extcon_dev_list_lock);
  1134. list_for_each_entry(edev, &extcon_dev_list, entry) {
  1135. if (edev->dev.parent && edev->dev.parent->of_node == node) {
  1136. mutex_unlock(&extcon_dev_list_lock);
  1137. of_node_put(node);
  1138. return edev;
  1139. }
  1140. }
  1141. mutex_unlock(&extcon_dev_list_lock);
  1142. of_node_put(node);
  1143. return ERR_PTR(-EPROBE_DEFER);
  1144. }
  1145. #else
  1146. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1147. {
  1148. return ERR_PTR(-ENOSYS);
  1149. }
  1150. #endif /* CONFIG_OF */
  1151. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  1152. /**
  1153. * extcon_get_edev_name() - Get the name of the extcon device.
  1154. * @edev: the extcon device
  1155. */
  1156. const char *extcon_get_edev_name(struct extcon_dev *edev)
  1157. {
  1158. return !edev ? NULL : edev->name;
  1159. }
  1160. static int __init extcon_class_init(void)
  1161. {
  1162. return create_extcon_class();
  1163. }
  1164. module_init(extcon_class_init);
  1165. static void __exit extcon_class_exit(void)
  1166. {
  1167. #if defined(CONFIG_ANDROID)
  1168. class_compat_unregister(switch_class);
  1169. #endif
  1170. class_destroy(extcon_class);
  1171. }
  1172. module_exit(extcon_class_exit);
  1173. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  1174. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  1175. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  1176. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1177. MODULE_DESCRIPTION("External connector (extcon) class driver");
  1178. MODULE_LICENSE("GPL");